当前位置:网站首页>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 !
边栏推荐
- Matlb| improved forward push back method for solving power flow of low voltage distribution network
- QStringList 的学习笔记
- MySQL enterprise parameter tuning practice sharing
- 用两个栈实现队列[两次先进后出便是先进先出]
- 数仓的字符截取三胞胎:substrb、substr、substring
- Software engineering job design (1): [personal project] implements a log view page
- Alchemy (3): how to do a good job in interfacing a business process
- Alchemy (6): iteratable models and use cases
- Leetcode 720. The longest word in the dictionary
- 炼金术(6): 可迭代的模型和用例
猜你喜欢

Flutter series: Transformers in flutter

现代编程语言:Rust (铁锈,一文掌握钢铁是怎样生锈的)

Redis主从复制、哨兵模式、集群的概述与搭建

Modern programming language: rust

剑指 Offer 65. 不用加减乘除做加法

Matlb| optimal configuration of microgrid in distribution system based on complex network

吴恩达《机器学习》课程总结(13)_聚类

Sword finger offer 61 Shunzi in playing cards

JVM的内存模型简介

免费、好用、强大的开源笔记软件综合评测
随机推荐
华泰证券在网上开户安全吗?
Technical debt wall: a way to make technical debt visible and negotiable
Code neatness -- format
[Reading Abstract] what is wrong with English Reading Teaching in schools-- Empiricism and the PK of cognitive science
Summary of wuenda's machine learning course (14)_ Dimensionality reduction
代码整洁之道--函数
flutter系列之:flutter中的变形金刚Transform
Leetcode 720. 词典中最长的单词(为啥感觉这道题很难?)
Alchemy (7): how to solve problems? Only reconstruction
炼金术(2): 为什么要用issue管理软件
#796 Div.2 F. Sanae and Giant Robot set *
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
On charsequence
Customize MySQL connection pool
哪个证券公司炒股开户是比较好,更安全靠谱的
用两个栈实现队列[两次先进后出便是先进先出]
数据仓库入门介绍
翻译(5): 技术债务墻:一种让技术债务可见并可协商的方法
代码整洁之道--格式
How many securities companies can a person open an account? Is it safe to open an account