当前位置:网站首页>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;
边栏推荐
- mysql econnreset_ Nodejs socket error handling error: read econnreset
- Reflection and imagination on the notation like tool
- Principle and configuration of RSTP protocol
- Flutter 绘制波浪移动动画效果,曲线和折线图
- Shuttle INKWELL & ink components
- 【Hot100】33. 搜索旋转排序数组
- 155. 最小栈
- OpenHarmony应用开发之Navigation组件详解
- Concurrent performance test of SAP Spartacus with JMeter
- [deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
猜你喜欢
Binder通信过程及ServiceManager创建过程
Fragmented knowledge management tool memos
Overflow toolbar control in SAP ui5 view
山东大学暑期实训一20220620
Association modeling method in SAP segw transaction code
Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
Write API documents first or code first?
About the single step debugging of whether SAP ui5 floating footer is displayed or not and the benefits of using SAP ui5
[深度学习论文笔记]TransBTSV2: Wider Instead of Deeper Transformer for Medical Image Segmentation
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
随机推荐
A detailed explanation of ASCII code, Unicode and UTF-8
峰会回顾|保旺达-合规和安全双驱动的数据安全整体防护体系
Shuttle INKWELL & ink components
Difference between avc1 and H264
Rocky基础知识1
SAE international strategic investment geometry partner
国际自动机工程师学会(SAE International)战略投资几何伙伴
PyCharm安装第三方库图解
Get to know linkerd project for the first time
Introduction aux contrôles de la page dynamique SAP ui5
946. Verify stack sequence
"Baidu Cup" CTF competition in September, web:sql
MySQL --- 数据库查询 - 排序查询、分页查询
Halcon template matching actual code (I)
#从源头解决# 自定义头文件在VS上出现“无法打开源文件“XX.h“的问题
量价虽降,商业银行结构性存款为何受上市公司所偏爱?
Default parameters of function & multiple methods of function parameters
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
Pycharm installation third party library diagram
Developers, is cloud native database the future?