当前位置:网站首页>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)
边栏推荐
- [data mining engineer - written examination] Haier company in 2022
- 采坑websocket總結
- 【LeetCode第 83 场双周赛】
- The way to access global variables in multi-source file mode (extern usage)
- 入职3个月的测试员面临转正,领导:1年工作经验包装成5年,试用期淘汰
- 【电赛训练】非接触物体尺寸形态测量 2020年电赛G题
- Redis | very important Middleware
- [the 83rd fortnight of leetcode]
- Problem note - unable to open include file: "direct.h": no such file or directory
- 测试小码农也有大目标,最新BAT大厂面试题大总结(持续更新中...)
猜你喜欢

XXL job realizes the code parsing of email sending warnings (line by line code interpretation)

数仓数据标准详解-2022

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

Difference between data index and label system of data warehouse

MySQL's heart index

Docker pulls the redis image and runs it

Introduction to QT (2.1 the first procedure for the beginning of QT)

Don't let Fujin Yibo see this

Small farmers also have big goals in the test, and the latest big bat interview summary (constantly updating...)

Comparison of the shortcomings of redis master-slave, sentinel and cluster architectures
随机推荐
Reverse linked list drawing demonstration
Classic examples of C language - use 4 × The matrix displays all integers from 1 to 16 and calculates the sum of each row, column, and diagonal
Table custom table encapsulation
Application of SCA on devsecops platform
MySQL之数据查询(SELECT)
T-seda code
Selection method of geometric objects in Creo 9.0
Flutter | the easiest way to add header and footer to listview
Case error of MySQL branch statement
Coloring old photos - deoldify get started quickly
XXL job realizes the code parsing of email sending warnings (line by line code interpretation)
Image processing 1:rgb888_ YCbCr444
暑假第四周总结
How to improve data quality
Customize an object
English grammar_ Demonstrative pronoun -such / the same
Design details related to sap e-commerce cloud Spartacus UI store
Overview of data model design method
Docker pulls the redis image and runs it
What is promise? What are the benefits of promise