当前位置:网站首页>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;
边栏推荐
- 百度杯”CTF比赛 2017 二月场,Web:爆破-2
- Binder通信过程及ServiceManager创建过程
- MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
- “百度杯”CTF比赛 九月场,Web:SQL
- PyCharm安装第三方库图解
- SAP ui5 objectpagelayout control usage sharing
- SAE international strategic investment geometry partner
- ABAP editor in SAP segw transaction code
- CloudCompare——点云切片
- Get to know linkerd project for the first time
猜你喜欢
Natural language processing series (I) introduction overview
STM32 and motor development (from architecture diagram to documentation)
How can non-technical departments participate in Devops?
“百度杯”CTF比赛 九月场,Web:SQL
Changing JS code has no effect
Word document injection (tracking word documents) incomplete
DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
碎片化知识管理工具Memos
解决 UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xa2 in position 107
[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
随机推荐
Natural language processing from Xiaobai to proficient (4): using machine learning to classify Chinese email content
Go pointer
Write macro with word
数据湖(七):Iceberg概念及回顾什么是数据湖
《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
Flutter InkWell & Ink组件
RHCSA10
Actual combat simulation │ JWT login authentication
Detailed explanation of navigation component of openharmony application development
Reverse Polish notation
手把手带你入门Apache伪静态的配置
Association modeling method in SAP segw transaction code
《2022年中國銀行業RPA供應商實力矩陣分析》研究報告正式啟動
CF:A. The Third Three Number Problem【关于我是位运算垃圾这个事情】
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]
使用Dom4j解析XML
CAN和CAN FD
RHCSA9
Shandong University Summer Training - 20220620
There is no monitoring and no operation and maintenance. The following is the commonly used script monitoring in monitoring