4.2.19. MySQL sorting

发布时间 :2025-10-25 12:31:20 UTC      

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.

Grammar

以下是 SQL SELECT 语句使用 ORDER BY 子句将查询数据排序后再返回数据:

SELECT field1, field2,...fieldN FROM table_name1, table_name2...
ORDER BY field1 [ASC [DESC][默认 ASC]], [field2...] [ASC [DESC][默认 ASC]]
  • 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.

Use the ORDER BY clause at the command prompt

The following will be used in the SQL SELECT statement ORDER BY Clause to read the MySQL data table runoob_tbl Data in:

Example

Try the following example and the results will be sorted in ascending and descending order.

SQL sorting

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)

Read runoob_tbl All data in the table and press submission_date The ascending order of fields.

Using the ORDER BY clause in PHP scripts

You can use the PHP function mysqli_query() And the same SQL SELECT with the command of the ORDER BY clause to get the data.

This function is used to execute the SQL command, and then use the PHP Function mysqli_fetch_array() To output data for all queries.

Example

Try the following example, and the data after query is pressed submission_date Fields are returned after being sorted in descending order.

MySQL ORDER BY Test:

<?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);?>

The output is shown in the following figure:

Image0

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.