当前位置:网站首页>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
Catalog
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)
边栏推荐
- 北峰通信亮相中国(厦门)应急展|智能化通信手段强势吸睛!
- Installation and use of appscan
- Classic example of C language - print the input two digits in reverse order
- Notes and Reflections on the red dust of the sky (V) strong culture and weak culture
- Case error of MySQL branch statement
- 工作3年的测试员跳槽后工资是原来的2倍,秘诀原来是......
- This is a big problem
- Redis | very important Middleware
- Selection method of geometric objects in Creo 9.0
- ES6 combines multiple class methods
猜你喜欢

How to improve data quality

Problem note - unable to open include file: "direct.h": no such file or directory

Classic examples of C language - adding two scores

Pbootcms database conversion tutorial (SQLite to MySQL detailed tutorial)

MySQL exercise: all employees reporting to the CEO

Classic example of C language - loan balance

Summary of polynomial commitment schemes

A good habit to develop when writing SQL

What is the function of the select... For UPDATE statement? Can you lock tables or rows?

Distributed cap principle
随机推荐
SAP 电商云 Spartacus UI Store 相关的设计明细
QT入门篇(2.1初入QT的开始第一个程序)
Interviewer: if the order is not paid within 30 minutes after it is generated, it will be automatically cancelled. How to realize it?
Notes and Reflections on the red dust of the sky (V) strong culture and weak culture
Accelerating matrix vector multiplication of special matrices with FFT
How to use SAP intelligent robotic process automation to automate Excel
AWS Article 3 how to publish message to IOT mqtt in go language
Are the top ten securities companies risky and safe to open accounts?
Table custom table encapsulation
Centernet target detection model and centerfusion fusion target detection model
Gbase 8C mode visibility query function (2)
C language: deep analysis of const keyword
Case error of MySQL branch statement
Customize an object
Focus on microservices
Simple implementation and analysis of binary search tree
[the 83rd fortnight of leetcode]
網絡系統實驗:ping不通的問題解决
Introduction to QT (2.1 the first procedure for the beginning of QT)
这是一道大水题