We know that SQL SELECT statements are used to read data from the MySQL table.
If we need to sort the read data, we can use the MySQL’s ORDER BY Clause to set which field and how you want to sort, and then return the search results. 以下是 SQL SELECT 语句使用 You can use any field as a condition for sorting to return the sorted query results. You can set multiple fields to sort. You can use the ASC or DESC keywords to set whether the query results are sorted in ascending or descending order. By default, it is arranged in ascending order. You can add a WHERE…LIKE clause to set the condition. The following will be used in the SQL SELECT statement Try the following example and the results will be sorted in ascending and descending order. SQL sorting Read You can use the PHP function This function is used to execute the SQL command, and then use the Try the following example, and the data after query is pressed MySQL ORDER BY Test: The output is shown in the following figure:Grammar ¶
ORDER
BY
子句将查询数据排序后再返回数据:SELECT field1, field2,...fieldN FROM table_name1, table_name2...
ORDER BY field1 [ASC [DESC][默认 ASC]], [field2...] [ASC [DESC][默认 ASC]]
Use the ORDER BY clause at the command prompt ¶
ORDER
BY
Clause to read the MySQL data table
runoob_tbl
Data in:Example ¶
mysql>useRUNOOB;Databasechangedmysql>SELECT\*fromrunoob_tblORDERBYsubmission_dateASC;
+-----------+---------------+---------------+-----------------+
\|runoob_id\|runoob_title\|runoob_author\|submission_date\|
+-----------+---------------+---------------+-----------------+ \|3\|
学习Java\|RUNOOB.COM\|2015-05-01\| \|4\|
学习Python\|RUNOOB.COM\|2016-03-06\| \|1\| 学习PHP\| 菜鸟教程
\|2017-04-12\| \|2\| 学习MySQL\| 菜鸟教程 \|2017-04-12\|
+-----------+---------------+---------------+-----------------+4rowsinset(0.01sec)mysql>SELECT\*fromrunoob_tblORDERBYsubmission_dateDESC;
+-----------+---------------+---------------+-----------------+
\|runoob_id\|runoob_title\|runoob_author\|submission_date\|
+-----------+---------------+---------------+-----------------+ \|1\|
学习PHP\| 菜鸟教程 \|2017-04-12\| \|2\| 学习MySQL\| 菜鸟教程
\|2017-04-12\| \|4\| 学习Python\|RUNOOB.COM\|2016-03-06\| \|3\|
学习Java\|RUNOOB.COM\|2015-05-01\|
+-----------+---------------+---------------+-----------------+4rowsinset(0.01sec)
runoob_tbl
All data in the table and press
submission_date
The ascending order of fields.Using the ORDER BY clause in PHP scripts ¶
mysqli_query()
And the same SQL SELECT with the command of the ORDER BY clause to get the data.
PHP
Function
mysqli_fetch_array()
To output data for all queries.Example ¶
submission_date
Fields are returned after being sorted in descending order.<?php$dbhost='localhost';//mysql服务器主机地址$dbuser='root';//mysql用户名$dbpass='123456';//mysql用户名密码$conn=mysqli_connect($dbhost,$dbuser,$dbpass);if(!$conn){die('连接失败:'.mysqli_error($conn));}//设置编码,防止中文乱码mysqli_query($conn,"set
names utf8");$sql='SELECT runoob_id, runoob_title, runoob_author,
submission_date FROM runoob_tbl ORDER BY submission_date
ASC';mysqli_select_db($conn,'RUNOOB');$retval=mysqli_query($conn,$sql);if(!$retval){die('无法读取数据:'.mysqli_error($conn));}echo'<h2>菜鸟教程
MySQL ORDER BY 测试<h2>';echo'<table border="1"><tr><td>教程
ID</td><td>标题</td><td>作者</td><td>提交日期</td></tr>';while($row=mysqli_fetch_array($retval,MYSQLI_ASSOC)){echo"<tr><td>{$row['runoob_id']}</td>"."<td>{$row['runoob_title']}</td>"."<td>{$row['runoob_author']}</td>"."<td>{$row['submission_date']}</td>"."</tr>";}echo'</table>';mysqli_close($conn);?>
