当前位置:网站首页>Latest MySQL advanced SQL statement Encyclopedia
Latest MySQL advanced SQL statement Encyclopedia
2022-06-28 00:38:00 【1024 Q】
introduction
One 、 Common query
1、order by Sort by keyword
1.1 Ascending sort
1.2 null
1.3 combination where Perform conditional filtering and then sort
1.4 Multi field sorting
2、and/or Judge
2.1 and/or And the use of and or
2.2 nesting 、 Many conditions of use
3、distinct Query does not duplicate records
4、GROUP BY Group results
5、limit Limit result entries
6、 Set alias (alias—as)
6.1 Number of records in the query table , Display as alias
6.2 utilize as, Import the queried data into another table
7、 wildcard
introductionYes MySQL Database query , In addition to basic queries , Sometimes you need to process the result set of the query . For example, take only 10 Data 、 Sort or group the query results, etc .
One 、 Common query 1、order by Sort by keywordUse SELECT Statement can transfer the required data from MySQL From the database , If you sort the results of the query , have access to order by Statement to sort statements , And finally return the sorted results to the user . The sorting of this statement can not only be for a certain field , You can also target multiple fields .
grammar :
select Field 1, Field 2... from Table name order by Field 1, Field 2... asc # Query results are displayed in ascending order ,asc It can be omitted select Field 1, Field 2... from Table name order by Field 1, Field 2,... desc # The query results are displayed in descending order ASC Is sorted in ascending order , Is the default sort method , namely ASC It can be omitted .SELECT If no specific sorting method is specified in the statement , By default ASC Sort by .DESC It is arranged in descending order . Of course order by You can also use where Clause to further filter the query results .
1.1 Ascending sortselect name ,score from report order by score;
select name ,score from report order by score desc;
select name,score,address from report where address='hangzhou' order by score;
ORDER BY Statements can also use multiple fields to sort , When there are multiple records with the same field in the first sorting , These multiple records are sorted according to the second field ,ORDER BY When followed by multiple fields , The fields are separated by English commas , Priority is in order , but order by The next first parameter is only when the same value appears , The second field makes sense .
select name,score,score2 from report order by score2,score desc;The experiment done here is Sort in ascending order and then in descending order , You can also sort both descending and ascending , There are not too many examples here .

In large databases , Sometimes querying data requires that the data meet certain characteristics and conditions
AND/OR And / or and shell Use the same method in the script
select name,score,cardid from report where score>80;
select name,score,cardid from report where score>80 and score<90;select name,score,cardid from report where score<60 or score>90;
select name,score,cardid from report where score<60 or (score>70 and score<80);select name,score,cardid from report where score<60 or (score>70 and score<=100) order by score desc;

select distinct Field from Table name ﹔ distinct Must be placed at the beginning distinct You can only use fields that need to be de duplicated distinct When removing multiple fields , The meaning is : Only when several fields are repeated at the same time can they be filtered , It will be based on the first field on the left by default .
adopt SQL The result of the search , You can also group them , Use GROUP BY Statement to implement ,GROUP BY Is usually Combining aggregate functions Used together , Common aggregation functions include : Count (COUNT)、 Sum up (SUM)、 Average (AVG)、 Maximum (MAX)、 minimum value (MIN),GROUP BY When grouping, the results can be grouped according to one or more fields .
Yes group by The query results of the following fields are summarized and grouped , Usually used in combination with aggregate functions
group by There is a principle , Namely select In all the following columns , Columns that do not use aggregate functions must appear in group by Behind .
select Field , Aggregate functions from Table name (where Field name ( matching ) The number ) group by Field name ;

limit Limit the result record of output
In the use of MySQL SELECT Statement to query , The result set returns all matching records ( That's ok ). Sometimes you just need to return the first line or the first few lines , And that's where it comes in LIMIT Clause .
Grammar format :select Field from Table name limit [offset,] numberlimit The first parameter is the position offset ( Optional parameters ), It's settings mysql Which line to start from if the first parameter is not set , It will start from the first record in the table . The first offset is 0, The second is 1offset Subscript for index number Is the number of digits after the index subscript 


stay mysql When inquiring , When the name of the table is long or some fields in the table are long , To facilitate writing or use the same table multiple times , You can set aliases for fields, columns, or tables , Convenient operation , enhance readability .
Alias of column select Field as Field alias Table name alias of the table select Alias . Field from Table name as Alias as It can be omitted Use scenarios :
When querying complex tables , Aliases can shorten the length of query statements when multiple tables are connected ( Easy to understand 、 Shorten sql sentence )
When setting an alias for a table , Make sure that the alias does not conflict with the names of other tables in the database .
The alias of the column is shown in the result , The alias of the table is not shown in the results , Use... Only when executing queries .


create table report2 as select * from report;select * from report2;select * from report;here as What it does :
Created a new table , And define the table structure , Insert table data ( And school The table is the same )
however ” constraint “ Not completely ” Copy “ To come over # But if the primary key is set in the original table , Then the attached table :default The field will be set to one by default 0


Wildcards are mainly used to replace some characters in a string , Query the relevant results through the matching of some characters .
Usually, wildcards are used with LIKE Used together , And cooperate with WHERE Clause to complete the query task .
There are two common wildcards , Namely :
%: A percent sign means zero 、 One or more characters _: The underline represents a single character 

wildcard “%” and “_” Not only can it be used alone , You can also combine

This is about MySQL senior SQL That's all for the statement article , More about mysql sql Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !
边栏推荐
- Alchemy (9): simple but not simple, never-ending test -- always_ run
- 技术的极限(11): 有趣的编程
- 投资场内ETF基金是靠谱吗,场内ETF基金安全吗
- 炼金术(2): 为什么要用issue管理软件
- Alchemy (2): why use issue management software
- 代码整洁之道--格式
- Validaterequest= "false" is a "suggestion collection" for what
- 炼金术(8): 开发和发布的并行
- Sell notes | brief introduction to video text pre training
- logging日志的使用
猜你喜欢

Promise是什么

MATLB|基于复杂网络的配电系统微电网优化配置
![计数质数[枚举 -> 空间换时间]](/img/11/c52e1dfce8e35307c848d12ccc6454.png)
计数质数[枚举 -> 空间换时间]

Hcip/hcie Routing & Switching / datacom Reference Dictionary Series (19) comprehensive summary of PKI knowledge points (public key infrastructure)

Modern programming languages: zig

Matlb| improved forward push back method for solving power flow of low voltage distribution network

LabVIEW continuous sampling and limited sampling mode

Sword finger offer 61 Shunzi in playing cards

Introduction to data warehouse

The limits of Technology (11): interesting programming
随机推荐
线程池实现:信号量也可以理解成小等待队列
internship:业务流程初识
用两个栈实现队列[两次先进后出便是先进先出]
Translation (4): matching rules for automatic text completion
線程池實現:信號量也可以理解成小等待隊列
#796 Div.2 C. Manipulating History 思维
智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
翻译(5): 技术债务墻:一种让技术债务可见并可协商的方法
[读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK
QStringList 的学习笔记
Matlb| optimal configuration of microgrid in distribution system based on complex network
flutter系列之:flutter中的变形金刚Transform
Understand the basic structure of wechat applet project
Count prime [enumeration - > space for time]
Internship: business process introduction
Is it safe for Huatai Securities to open an account online?
SCU|通过深度强化学习进行微型游泳机器人的步态切换和目标导航
SQL reported an unusual error, which confused the new interns
#795 Div.2 E. Number of Groups set *
积分体系和营销活动结合在一起有哪些玩法