当前位置:网站首页>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:
ASCIndicates that the fields are sorted in ascending order ;DESCIndicates that the fields are sorted in descending order . among [ ]Represents optional keywords ASCAs 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;
边栏推荐
- 初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。
- #yyds干货盘点# 解决名企真题:搬圆桌
- 量价虽降,商业银行结构性存款为何受上市公司所偏爱?
- 蜀天梦图×微言科技丨达梦图数据库朋友圈+1
- Notion 类笔记软件如何选择?Notion 、FlowUs 、Wolai 对比评测
- Pycharm installation third party library diagram
- Developers, is cloud native database the future?
- Write API documents first or code first?
- My colleague didn't understand selenium for half a month, so I figured it out for him in half an hour! Easily showed a wave of operations of climbing Taobao [easy to understand]
- Alibaba cloud SLB load balancing product basic concept and purchase process
猜你喜欢

Cf:a. the third three number problem

Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1

RHCSA8

Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications

SAP UI5 DynamicPage 控件介紹

Get to know linkerd project for the first time

jenkins安装

CF:A. The Third Three Number Problem【关于我是位运算垃圾这个事情】

解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107

Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
随机推荐
APICloud Studio3 WiFi真机同步和WiFi真机预览使用说明
SAP ui5 objectpagelayout control usage sharing
A specific example of ABAP type and EDM type mapping in SAP segw transaction code
go 指针
Flutter InkWell & Ink组件
精彩速递|腾讯云数据库6月刊
时钟周期
#yyds干货盘点# 解决名企真题:搬圆桌
RHCSA10
Lb10s-asemi rectifier bridge lb10s
程序员成长第八篇:做好测试工作
Binder通信过程及ServiceManager创建过程
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
Go string operation
Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications
实现 1~number 之间,所有数字的加和
My colleague didn't understand selenium for half a month, so I figured it out for him in half an hour! Easily showed a wave of operations of climbing Taobao [easy to understand]
事务的基本特性和隔离级别
Talk about seven ways to realize asynchronous programming
go 字符串操作