数据库教程-mariadb教程-MariaDB Order By子句

跨零代码为大家提供高品质的解决方案,请大家多多来访,跨零不胜感激,在此谢过。

在MariaDB数据库中,ORDER BY子句用于按升序或降序对结果集中的记录进行排序。

语法:

SELECT expressions   FROM tables   [WHERE conditions]   ORDER BY expression [ ASC | DESC ]; 

1. ORDER BY子句不使用ASC/DESC属性

在这个示例中,使用具有以下数据的students表:

MariaDB [testdb]> select * from students; +------------+--------------+-----------------+----------------+ | student_id | student_name | student_address | admission_date | +------------+--------------+-----------------+----------------+ |          1 | Maxsu        | Haikou          | 2017-01-07     | |          3 | JMaster      | Beijing         | 2016-05-07     | |          4 | Mahesh       | Guangzhou       | 2016-06-07     | |          5 | Kobe         | Shanghai        | 2016-02-07     | |          6 | Blaba        | Shengzhen       | 2016-08-07     | |          7 | Maxsu        | Sanya           | 2017-08-08     | +------------+--------------+-----------------+----------------+ 6 rows in set (0.00 sec) 

示例

SELECT * FROM students   WHERE student_name LIKE '%Ma%'   ORDER BY student_id; 

执行上面查询语句,得到以下结果 –

MariaDB [testdb]> SELECT * FROM students     -> WHERE student_name LIKE '%Ma%'     -> ORDER BY student_id; +------------+--------------+-----------------+----------------+ | student_id | student_name | student_address | admission_date | +------------+--------------+-----------------+----------------+ |          1 | Maxsu        | Haikou          | 2017-01-07     | |          3 | JMaster      | Beijing         | 2016-05-07     | |          4 | Mahesh       | Guangzhou       | 2016-06-07     | |          7 | Maxsu        | Sanya           | 2017-08-08     | +------------+--------------+-----------------+----------------+ 4 rows in set (0.00 sec) 

在上面结果集中,可以看到是按student_id字段从小到大(不指定ASCDESC时,默认使用ASC)来排序的。

2. ORDER BY子句使用DESC属性

示例

SELECT * FROM students   WHERE student_name LIKE '%Ma%'   ORDER BY student_id DESC; 

执行上面查询语句,得到以下结果 –

MariaDB [testdb]> SELECT * FROM students     -> WHERE student_name LIKE '%Ma%'     -> ORDER BY student_id DESC; +------------+--------------+-----------------+----------------+ | student_id | student_name | student_address | admission_date | +------------+--------------+-----------------+----------------+ |          7 | Maxsu        | Sanya           | 2017-08-08     | |          4 | Mahesh       | Guangzhou       | 2016-06-07     | |          3 | JMaster      | Beijing         | 2016-05-07     | |          1 | Maxsu        | Haikou          | 2017-01-07     | +------------+--------------+-----------------+----------------+ 4 rows in set (0.00 sec) 

在上面结果集中,可以看到是按student_id字段从大到小(指定DESC)来排序的。

3. 按多列排序

假设students表中有两个人的名字是:Maxsu,我们希望先按student_name升序排序,在列的值相同时,再按student_id降序排序,参考以下查询语句 –

SELECT * FROM students   WHERE student_name LIKE '%Ma%'   ORDER BY student_name ASC, student_id DESC; 

执行上面查询语句,得到以下结果 –

MariaDB Order By子句

  

从零到一,创造未来!跨零综合IT问题解决服务站,欢迎你的到来。Oracle数据库教程 mariadb教程 只为你绽放。

本文固定链接: http://kua0.com/2019/01/31/数据库教程-mariadb教程-mariadb-order-by子句/

为您推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注