当前位置:网站首页>MySQL - database query - sort query, paging query
MySQL - database query - sort query, paging query
2022-07-05 13:18:00 【Sauerkraut】
Sort query
Through the conditional query statement, you can query the data that meets the needs of users , However, the queried data is generally displayed in the order in which the data was initially added to the table , What is added first is displayed first , Added after the display . In order to make the order of query results meet the requirements of users ,MySQL Provides ORDER BY Keyword to sort the query results .
In practical applications, it is often necessary to sort the query results , such as , When shopping online , The goods can be priced 、 Sort by quantity ; In the hospital registration system , You can sort according to the order of registration .
grammar
SELECT Field name FROM Table name ... ORDER BY Sort field names [ASC|DESC],[ Sort field names [ASC|DESC]];
Syntax description
Sort field names : Indicates the name of the field to be sorted , If there are multiple fields separated by commas .
Sort rule :ASC|DESC:
ASC
Indicates that the fields are sorted in ascending order ;DESC
Indicates that the fields are sorted in descending order . among [ ]Represents optional keywords ASC
As the default value , The default is in ascending order .
characteristic
ORDER BY Clauses are usually placed at the end of the query statement ,LIMIT Except for words .
When there is a null value in the sorted field NULL when ,ORDER BY The null value will be treated as the minimum value .
ORDER BY When specifying multiple fields to sort ,MySQL It will be sorted from left to right according to the order of fields . for example , At first according to job Sort , When job When there are duplicate values , It will be sorted by the next specified field
Be careful : When sorting multiple fields , The first field of sorting must have the same value , To sort the second field . If all values in the first field data are unique ,MySQL The second field will no longer be sorted .
Query requirement
1. Sort employees in ascending order by name
SELECT * FROM emp ORDER BY ename ASC;
2. The query entry time is greater than 1981 year 6 month 6 Daily employee information , And in ascending order of employment time
-- SQL surface --
-- The query entry time is greater than 1981 year 6 month 6 Daily employee information
SELECT * FROM emp WHERE hiredate>'1981-6-6';
-- The query entry time is greater than 1981 year 6 month 6 Daily employee information , And in ascending order of employment time
SELECT * FROM emp WHERE hiredate>'1981-6-6' ORDER BY hiredate ASC;
3. According to the job position , In ascending order , With the same position , Then arrange them in descending order according to the employment time
Sort a field first , If the first sorting field is repeated , Just sort by the second field
-- SQL surface --
-- Arrange in ascending order according to the job position
SELECT * FROM emp ORDER BY job ASC;
-- Arrange in ascending order according to the job position , In the same position , Then arrange them in descending order according to the employment time Separate the fields to be sorted with commas
SELECT * FROM emp ORDER BY job ASC,hiredate DESC;
4. The salary inquiry is not available 1000 To 2000 The name and salary of the employee between , Wages are in descending order
-- SQL surface --
-- The salary inquiry is not available 1000 To 2000 The name and salary of the employee between
SELECT ename,sal FROM emp WHERE sal NOT BETWEEN 1000 AND 2000;
-- The salary inquiry is not available 1000 To 2000 The name and salary of the employee between , Wages are in descending order
SELECT ename,sal FROM emp WHERE sal NOT BETWEEN 1000 AND 2000 ORDER BY sal DESC;
5. Group by job position , Check the number of people in each job position , Jobs are arranged in ascending order
-- SQL surface --
-- Group by job position , Check the number of people in each job position
SELECT job,COUNT(*) FROM emp GROUP BY job;
-- Group by job position , Check the number of people in each job position , Jobs are arranged in ascending order
SELECT job,COUNT(*) FROM emp GROUP BY job ORDER BY job ASC;
Paging query
In the previous query , Generally, all fields are directly queried , For more data , If all are displayed on one page , It will be dazzling to check . If you can display in pages , Will be faster 、 Fresh browsing !
grammar
SELECT Field name FROM Table name LIMIT Starting index , Number of query records ;
characteristic
Starting index from 0 Numbered starting , Starting index = ( Look up the page number - 1) * Each page shows the number of records
Yes 5 Data ,3 Data is divided into one page , If the query is the first page of data , The starting index can be omitted , Directly abbreviated as LIMIT 3, If you are querying the second page of data , Starting index from 3 Start , The number of query records can be written directly 3, If it is missing, only two will be displayed , If there is more, only 3 strip
Query requirement
1. Query the employee data on the first page , Each page is displayed 10 Bar record
-- Query the employee data on the first page , Each page is displayed 10 Bar record
SELECT * FROM emp LIMIT 0,10;
-- Query the employee data on the second page , Each page shows 10 Data
SELECT * FROM emp LIMIT 10,10;
-- Starting index from 0 Start , Starting index = ( Look up the page number - 1) * Each page shows the number of records (2-1)*10
2. Query salary greater than 1500 Employee data for , In ascending order of salary , And paging , Each page is displayed 5 Data
-- Query salary greater than 1500 Employee data for , Sort and sort by salary ascending order , Each page is displayed 5 Data
SELECT * FROM emp WHERE sal>1500 ORDER BY sal ASC LIMIT 0,5;
边栏推荐
- Leetcode20. Valid parentheses
- Rocky basics 1
- Shandong University Summer Training - 20220620
- 聊聊异步编程的 7 种实现方式
- Write API documents first or code first?
- SAP UI5 DynamicPage 控件介紹
- It's too convenient. You can complete the code release and approval by nailing it!
- Rocky基础知识1
- Flutter draws animation effects of wave movement, curves and line graphs
- 蜀天梦图×微言科技丨达梦图数据库朋友圈+1
猜你喜欢
先写API文档还是先写代码?
Principle and performance analysis of lepton lossless compression
ASEMI整流桥HD06参数,HD06图片,HD06应用
Put functions in modules
Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
Introduction aux contrôles de la page dynamique SAP ui5
CAN和CAN FD
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
[深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2
简单上手的页面请求和解析案例
随机推荐
事务的基本特性和隔离级别
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
Rocky basic command 3
go 字符串操作
山东大学暑期实训一20220620
SAP ui5 objectpagelayout control usage sharing
RHCSA8
Concurrent performance test of SAP Spartacus with JMeter
There is no monitoring and no operation and maintenance. The following is the commonly used script monitoring in monitoring
Reflection and imagination on the notation like tool
逆波兰表达式
[深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2
Hiengine: comparable to the local cloud native memory database engine
ASEMI整流桥HD06参数,HD06图片,HD06应用
初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。
Fragmented knowledge management tool memos
AVC1与H264的区别
946. 验证栈序列
Get to know linkerd project for the first time