4.2.19. MySQL sorting

发布时间 : 2025-10-25 13:32:49 UTC      

Page Views: 10 views

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:

菜鸟教程 MySQL ORDER BY 测试

';echo'';while($row=mysqli_fetch_array($retval,MYSQLI_ASSOC)){echo"".""."".""."";}echo'
教程 ID标题作者提交日期
{$row['runoob_id']}{$row['runoob_title']}{$row['runoob_author']}{$row['submission_date']}
';mysqli_close($conn);?>

The output is shown in the following figure:

Image0

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。