当前位置:网站首页>Tutorial on principles and applications of database system (043) -- MySQL query (V): Sorting Query Results

Tutorial on principles and applications of database system (043) -- MySQL query (V): Sorting Query Results

2022-07-24 00:49:00 Rsda DBA_ WGX

Database system principle and Application Tutorial (043)—— MySQL Inquire about ( 5、 ... and ): Sort query results

Use in query ORDER BY Clause can sort query results . By default, records are sorted in ascending order , If you want to sort records in descending order , have access to DESC keyword .

The syntax is as follows :

--  among 【 Column name or expression 】 You can use serial numbers instead of 
ORDER BY
 Column name or expression  [DESC] [,...]

One 、 Sort using column names

for example :

(1) Query the information of the student table , according to birth Ascending order .

mysql> select * from student order by birth;
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2013 |  Cao mengde     | 1998-02-13 00:00:00 | 13853735522 |  Zhengzhou city     |
| S2014 |  Liu Yan       | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2026 |  Zhang Xueyou     | 1998-07-06 00:00:00 | 13743735566 |  Zhengzhou city     |
| S2022 |  Zhou Huajian     | 1999-05-25 00:00:00 | 13243735578 |  Zhengzhou city     |
| S2023 |  trump     | 1999-06-21 00:00:00 | 13343735588 |  Xinxiang City     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2031 |  Lee Myung Bak     | 1999-10-26 00:00:00 | 13643732222 |  Zhengzhou city     |
| S2011 |  Zhang Xiaogang     | 1999-12-03 00:00:00 | 13163735775 |  Xinyang City     |
| S2032 |  Vinci     | 1999-12-31 00:00:00 | 13043731234 |  Zhengzhou city     |
| S2021 |  Dong Wenhua     | 2000-07-30 00:00:00 | 13533735564 |  Kaifeng City     |
| S2025 |  Zhou Jianhua     | 2000-08-22 00:00:00 | 13788736655 |  Kaifeng City     |
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
+-------+-----------+---------------------+-------------+-----------+
14 rows in set (0.01 sec)

(2) Query the information of the student table , according to phone Descending order .

mysql> select * from student order by phone desc;
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2013 |  Cao mengde     | 1998-02-13 00:00:00 | 13853735522 |  Zhengzhou city     |
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2025 |  Zhou Jianhua     | 2000-08-22 00:00:00 | 13788736655 |  Kaifeng City     |
| S2026 |  Zhang Xueyou     | 1998-07-06 00:00:00 | 13743735566 |  Zhengzhou city     |
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
| S2031 |  Lee Myung Bak     | 1999-10-26 00:00:00 | 13643732222 |  Zhengzhou city     |
| S2014 |  Liu Yan       | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2021 |  Dong Wenhua     | 2000-07-30 00:00:00 | 13533735564 |  Kaifeng City     |
| S2023 |  trump     | 1999-06-21 00:00:00 | 13343735588 |  Xinxiang City     |
| S2022 |  Zhou Huajian     | 1999-05-25 00:00:00 | 13243735578 |  Zhengzhou city     |
| S2011 |  Zhang Xiaogang     | 1999-12-03 00:00:00 | 13163735775 |  Xinyang City     |
| S2032 |  Vinci     | 1999-12-31 00:00:00 | 13043731234 |  Zhengzhou city     |
+-------+-----------+---------------------+-------------+-----------+
14 rows in set (0.00 sec)

(3) Query the information of the student table , At the same time, in accordance with the addr Ascending 、phone Descending order .

mysql> select * from student order by addr, phone desc;
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2011 |  Zhang Xiaogang     | 1999-12-03 00:00:00 | 13163735775 |  Xinyang City     |
| S2025 |  Zhou Jianhua     | 2000-08-22 00:00:00 | 13788736655 |  Kaifeng City     |
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
| S2021 |  Dong Wenhua     | 2000-07-30 00:00:00 | 13533735564 |  Kaifeng City     |
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2023 |  trump     | 1999-06-21 00:00:00 | 13343735588 |  Xinxiang City     |
| S2013 |  Cao mengde     | 1998-02-13 00:00:00 | 13853735522 |  Zhengzhou city     |
| S2026 |  Zhang Xueyou     | 1998-07-06 00:00:00 | 13743735566 |  Zhengzhou city     |
| S2031 |  Lee Myung Bak     | 1999-10-26 00:00:00 | 13643732222 |  Zhengzhou city     |
| S2014 |  Liu Yan       | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2022 |  Zhou Huajian     | 1999-05-25 00:00:00 | 13243735578 |  Zhengzhou city     |
| S2032 |  Vinci     | 1999-12-31 00:00:00 | 13043731234 |  Zhengzhou city     |
+-------+-----------+---------------------+-------------+-----------+
14 rows in set (0.00 sec)

(4) Query the information of the student table , At the same time, in accordance with the addr Descending sum phone Descending order .

mysql> select * from student order by addr desc, phone desc;
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2013 |  Cao mengde     | 1998-02-13 00:00:00 | 13853735522 |  Zhengzhou city     |
| S2026 |  Zhang Xueyou     | 1998-07-06 00:00:00 | 13743735566 |  Zhengzhou city     |
| S2031 |  Lee Myung Bak     | 1999-10-26 00:00:00 | 13643732222 |  Zhengzhou city     |
| S2014 |  Liu Yan       | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2022 |  Zhou Huajian     | 1999-05-25 00:00:00 | 13243735578 |  Zhengzhou city     |
| S2032 |  Vinci     | 1999-12-31 00:00:00 | 13043731234 |  Zhengzhou city     |
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2023 |  trump     | 1999-06-21 00:00:00 | 13343735588 |  Xinxiang City     |
| S2025 |  Zhou Jianhua     | 2000-08-22 00:00:00 | 13788736655 |  Kaifeng City     |
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
| S2021 |  Dong Wenhua     | 2000-07-30 00:00:00 | 13533735564 |  Kaifeng City     |
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2011 |  Zhang Xiaogang     | 1999-12-03 00:00:00 | 13163735775 |  Xinyang City     |
+-------+-----------+---------------------+-------------+-----------+
14 rows in set (0.00 sec)

Two 、 Sort using expressions

for example :

(1) Query the information of the student table , according to age( from birth It is calculated that ) Ascending order .

mysql> select * from student order by year(now())-year(birth);
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
| S2021 |  Dong Wenhua     | 2000-07-30 00:00:00 | 13533735564 |  Kaifeng City     |
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
| S2025 |  Zhou Jianhua     | 2000-08-22 00:00:00 | 13788736655 |  Kaifeng City     |
| S2011 |  Zhang Xiaogang     | 1999-12-03 00:00:00 | 13163735775 |  Xinyang City     |
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2022 |  Zhou Huajian     | 1999-05-25 00:00:00 | 13243735578 |  Zhengzhou city     |
| S2023 |  trump     | 1999-06-21 00:00:00 | 13343735588 |  Xinxiang City     |
| S2031 |  Lee Myung Bak     | 1999-10-26 00:00:00 | 13643732222 |  Zhengzhou city     |
| S2032 |  Vinci     | 1999-12-31 00:00:00 | 13043731234 |  Zhengzhou city     |
| S2013 |  Cao mengde     | 1998-02-13 00:00:00 | 13853735522 |  Zhengzhou city     |
| S2014 |  Liu Yan       | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2026 |  Zhang Xueyou     | 1998-07-06 00:00:00 | 13743735566 |  Zhengzhou city     |
+-------+-----------+---------------------+-------------+-----------+
14 rows in set (0.00 sec)

(2) Query the information of the student table , according to phone After 4 Bits in descending order .

mysql> select * from student order by right(phone,4) desc;
+-------+-----------+---------------------+-------------+-----------+
| s_id  | s_name    | birth               | phone       | addr      |
+-------+-----------+---------------------+-------------+-----------+
| S2025 |  Zhou Jianhua     | 2000-08-22 00:00:00 | 13788736655 |  Kaifeng City     |
| S2024 |  Obama     | 2000-10-17 00:00:00 | 13843735885 |  Xinyang City     |
| S2011 |  Zhang Xiaogang     | 1999-12-03 00:00:00 | 13163735775 |  Xinyang City     |
| S2023 |  trump     | 1999-06-21 00:00:00 | 13343735588 |  Xinxiang City     |
| S2022 |  Zhou Huajian     | 1999-05-25 00:00:00 | 13243735578 |  Zhengzhou city     |
| S2026 |  Zhang Xueyou     | 1998-07-06 00:00:00 | 13743735566 |  Zhengzhou city     |
| S2021 |  Dong Wenhua     | 2000-07-30 00:00:00 | 13533735564 |  Kaifeng City     |
| S2016 |  Liu Ruofei     | 2000-08-31 00:00:00 | 13683735533 |  Kaifeng City     |
| S2013 |  Cao mengde     | 1998-02-13 00:00:00 | 13853735522 |  Zhengzhou city     |
| S2014 |  Liu Yan       | 1998-06-24 00:00:00 | 13623735335 |  Zhengzhou city     |
| S2015 |  Liu Yan       | 1999-07-06 00:00:00 | 13813735225 |  Xinyang City     |
| S2012 |  Liu Xiaoqing     | 1999-10-11 00:00:00 | 13603732255 |  Xinxiang City     |
| S2031 |  Lee Myung Bak     | 1999-10-26 00:00:00 | 13643732222 |  Zhengzhou city     |
| S2032 |  Vinci     | 1999-12-31 00:00:00 | 13043731234 |  Zhengzhou city     |
+-------+-----------+---------------------+-------------+-----------+
14 rows in set (0.01 sec)

3、 ... and 、 Use numbers instead of column names or expressions

stay ORDER BY Clause can use numbers instead of column names or expressions , Numbers n The meaning of is that the column name or expression is in SELECT Sequence number in Clause . The column or expression represented by the numerical sequence number must appear in SELECT clause .

for example :

(1) Search for student information , according to age Descending order .

-- order by  hinder  3  It means according to  select  Number... In the list  3  Sort options 
mysql> select s_id, s_name, year(now()) - year(birth) age
    -> from student order by 3;
+-------+-----------+------+
| s_id  | s_name    | age  |
+-------+-----------+------+
| S2016 |  Liu Ruofei     |   22 |
| S2021 |  Dong Wenhua     |   22 |
| S2024 |  Obama     |   22 |
| S2025 |  Zhou Jianhua     |   22 |
| S2011 |  Zhang Xiaogang     |   23 |
| S2012 |  Liu Xiaoqing     |   23 |
| S2015 |  Liu Yan       |   23 |
| S2022 |  Zhou Huajian     |   23 |
| S2023 |  trump     |   23 |
| S2031 |  Lee Myung Bak     |   23 |
| S2032 |  Vinci     |   23 |
| S2013 |  Cao mengde     |   24 |
| S2014 |  Liu Yan       |   24 |
| S2026 |  Zhang Xueyou     |   24 |
+-------+-----------+------+
14 rows in set (0.00 sec)

(2) Search for student information , according to addr Descending sum phone after 4 Bits in descending order .

-- order by  hinder  4  Express  select  The fourth item in the clause (addr),3  Express  select  Clause  3  term ( expression )
mysql> select s_id, s_name, right(phone,4) phone4, addr
    -> from student order by 4 desc, 3 desc;
+-------+-----------+--------+-----------+
| s_id  | s_name    | phone4 | addr      |
+-------+-----------+--------+-----------+
| S2022 |  Zhou Huajian     | 5578   |  Zhengzhou city     |
| S2026 |  Zhang Xueyou     | 5566   |  Zhengzhou city     |
| S2013 |  Cao mengde     | 5522   |  Zhengzhou city     |
| S2014 |  Liu Yan       | 5335   |  Zhengzhou city     |
| S2031 |  Lee Myung Bak     | 2222   |  Zhengzhou city     |
| S2032 |  Vinci     | 1234   |  Zhengzhou city     |
| S2023 |  trump     | 5588   |  Xinxiang City     |
| S2012 |  Liu Xiaoqing     | 2255   |  Xinxiang City     |
| S2025 |  Zhou Jianhua     | 6655   |  Kaifeng City     |
| S2021 |  Dong Wenhua     | 5564   |  Kaifeng City     |
| S2016 |  Liu Ruofei     | 5533   |  Kaifeng City     |
| S2024 |  Obama     | 5885   |  Xinyang City     |
| S2011 |  Zhang Xiaogang     | 5775   |  Xinyang City     |
| S2015 |  Liu Yan       | 5225   |  Xinyang City     |
+-------+-----------+--------+-----------+
14 rows in set (0.00 sec)
原网站

版权声明
本文为[Rsda DBA_ WGX]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230648567084.html