当前位置:网站首页>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 !
边栏推荐
- Matlab基本函数 length函数
- flutter系列之:flutter中的变形金刚Transform
- 现代编程语言:Rust (铁锈,一文掌握钢铁是怎样生锈的)
- 互联网业衍生出来了新的技术,新的模式,新的产业类型
- #795 Div.2 D. Max GEQ Sum 单调栈
- Summary of wuenda's machine learning course (11)_ Support vector machine
- Sword finger offer 61 Shunzi in playing cards
- Flutter series: Transformers in flutter
- RNA-seq入门实战(一):上游数据下载、格式转化和质控清洗
- SQL reported an unusual error, which confused the new interns
猜你喜欢
随机推荐
The development of the Internet provides new solutions for industrial transformation
Acwing第 57 场周赛【未完结】
哪个证券公司炒股开户是比较好,更安全靠谱的
【论文阅读|深读】SDNE:Structural Deep Network Embedding
Technical debt wall: a way to make technical debt visible and negotiable
zotero文献管理工具安装使用
MATLAB basic function length function
Startup and shutdown of Oracle Database
Flutter series: Transformers in flutter
[黑苹果系列] M910x完美黑苹果系统安装教程 – 2 制作系统U盘-USB Creation
ValidateRequest=”false” 是做什么的「建议收藏」
现代编程语言:zig
LabVIEW连续采样与有限采样模式
Which securities company is better and safer to open an account for stock speculation
How many securities companies can a person open an account? Is it safe to open an account
云厂商为什么都在冲这个KPI?
什么是cookie,以及v-htm的安全性隐患
Alchemy (6): iteratable models and use cases
HCIP/HCIE Routing&Switching / Datacom备考宝典系列(十九)PKI知识点全面总结(公钥基础架构)
用两个栈实现队列[两次先进后出便是先进先出]

![用两个栈实现队列[两次先进后出便是先进先出]](/img/de/07297816f1a44d41389bb45d012c80.png)







