当前位置:网站首页>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)
边栏推荐
- High number_ Chapter 2 differential calculus of multivariate functions__ Geometric application of partial derivatives_ Tangent and normal plane of space curve
- Bean validation custom container validation chapter ----06
- Are the top ten securities companies risky and safe to open accounts?
- Starfish OS: create a new paradigm of the meta universe with reality as the link
- Gbase 8C access authority query function (6)
- 如何在自动化测试中使用MitmProxy获取数据返回?
- Bean Validation使用篇----05
- Gbase 8C mode visibility query function (2)
- Problem note - unable to open include file: "direct.h": no such file or directory
- MySQL之数据查询(SELECT)
猜你喜欢

How can dbcontext support the migration of different databases in efcore advanced SaaS system

Application of SCA on devsecops platform

Selection method of geometric objects in Creo 9.0

Redis master-slave synchronization mechanism

The prediction of domestic AI protein structure reproduced a breakthrough and solved the 3D structure with a single sequence. Peng Jian's team: "the last piece of puzzle since alphafold2 has been comp

EFCore高级Saas系统下单DbContext如何支持不同数据库的迁移

網絡系統實驗:ping不通的問題解决

Summary of polynomial commitment schemes

postman测试接口在URL配置正确的情况下出现404或者500错误

This is a big problem
随机推荐
mysql 分支语句case报错
Flutter | the easiest way to add header and footer to listview
SAP 实施项目中涉及到编程方式操作 Excel 的几种场景介绍
Simple implementation and analysis of binary search tree
【数据挖掘工程师-笔试】2022年海尔 公司
Blockbuster | certik: Web3.0 industry safety report release in the second quarter of 2022 (PDF download link attached)
[the 83rd fortnight of leetcode]
数仓数据标准详解-2022
Sed in-depth understanding and use
Image processing 1:rgb888_ YCbCr444
Testers who have been employed for 3 months are facing employment confirmation. Leaders: 1 year of work experience is packaged into 5 years, and the probation period is eliminated
T-seda code
Multi data source configuration of mongodb
Classic examples of C language switch case statement conversion date format
Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?
A good habit to develop when writing SQL
PostgreSQL snapshot optimization globalvis new system analysis (greatly enhanced performance)
Semaphore
MariaDB database upgrade version
English grammar_ Demonstrative pronoun -such / the same