当前位置:网站首页>Data query language (DQL)
Data query language (DQL)
2022-07-25 09:29:00 【__ Samual】
Catalog
2. Query multiple columns of data
3、 ... and 、 Multi-table query
2. Equivalent connection and non equivalent connection
3. Internal and external connections
4. Left connection and right connection
Inquire about Take the data out of the table , Put it in a temporary table .
There are three ways to query ( choice , Projection Select, Connect from union )
The writing order of query statements is as follows
Select Clause from Clause
where Clause
order by Sort
having Clause
group by grouping
union Connect
The execution order of query statements is as follows
FROM WHERE GROUP BY HAVING SELECT ORDER BY
1. From First, determine the source of data retrieval --- surface
2. Where Determine the row corresponding to the qualification --- That's ok
3. Select Confirm that the data to be retrieved is projected to the corresponding column
It is known that student The database contains student,sc,course Three tables , The following example will query this table :



One 、 Basic query
1. Query single column data
grammar :
select Name from Table name ;Case study :
Query student ID
select Sno from student;
2. Query multiple columns of data
grammar :
select Name 1, Name 2,..... Name n from Table name ;Be careful : Between column names, use ' , ' separate .
Case study :
Check student number and student name
select Sno.Sname from student;
3. Query all the data
grammar :
select * from Table name ;Case study :
Query form student All data in
select * from student;
4. De duplication query data
grammar :
select distinct Name from Table name ;Be careful : If the keyword is followed by more than one search column Then you need to retrieve all the data in the column to remove the duplicate .
Case study :
Check the student's name
select distinct Sname from student;
Two 、 Conditions of the query
1. Conditions of the query
grammar :
select Name from Table name where Query criteria ;Case study :
see student The age in the table is 20 Student name of .

2. Fuzzy query
Fuzzy query keyword :like;
% Indicates zero or more characters
_ Indicates that there is only one character
like Clause and where Equal sign of clause ‘=’ The effect is the same
grammar :
select Name from Table name where Name like Query criteria ;Case study :
Inquire about student The student surnamed Wang in the table .

3. Regular expressions
Mysql Regular expressions for use REGEXP Use this keyword to specify the matching pattern of the regular expression ; To add ' '.
| Pattern | describe |
|---|---|
| ^ | Matches the start of the input string . If set RegExp Object's Multiline attribute ,^ Also match '\n' or '\r' The position after . |
| $ | Matches the end of the input string . If set RegExp Object's Multiline attribute ,$ Also match '\n' or '\r' Previous position . |
| . | Matching elimination "\n" Any single character other than . To match includes '\n' Any character inside , Please use the image '[.\n]' The pattern of . |
| [...] | Character set . Matches any of the contained characters . for example , '[abc]' Can match "plain" Medium 'a'. |
| [^...] | Negative character set . Match any character not included . for example , '[^abc]' Can match "plain" Medium 'p'. |
| p1|p2|p3 | matching p1 or p2 or p3. for example ,'z|food' Can match "z" or "food".'(z|f)ood' The match "zood" or "food". |
| * | Match previous subexpression zero or more times . for example ,zo* Can match "z" as well as "zoo".* Equivalent to {0,}. |
| + | Match previous subexpression one or more times . for example ,'zo+' Can match "zo" as well as "zoo", But can't match "z".+ Equivalent to {1,}. |
| {n} | n Is a non negative integer . Matched definite n Time . for example ,'o{2}' Can't match "Bob" Medium 'o', But it matches "food" Two of them o. |
| {n,m} | m and n All non negative integers , among n <= m. Least match n Times and at most m Time . |
grammar :
select Name from Table name where Name regexp ' Conditions ';4. Sort and limit :
Sort : If we need to sort the read data , We can use MySQL Of ORDER BY Clause to set which fields and how you want to sort , Return to search results .
grammar :
select Query list from Table name where filter order by Sort list asc / desc ;asc: Ascending , If you don't write the default ascending order
desc: Descending
Case study :
take sc The scores in the table are sorted in ascending and descending order


Limit : You can limit the number of query results , stay MySQL Use in limit To limit the query .
grammar :
select * from Table name limit Number ;Case study
Inquire about sc The top three scores in the table

3、 ... and 、 Multi-table query
1. The cartesian product
Each row of a table is connected with each row of the second table
grammar :
select * from surface 1, surface 2;Be careful : When there are no restrictions on the connection of two meters , The connected table is N*M That's ok
Case study :
take student and sc Two tables are connected by Cartesian product

2. Equivalent connection and non equivalent connection
The condition used to connect two tables in a join query is called a join condition , Its general format is :
Table name 1. Field name 1 Comparison operator Table name 2. Field name 2;Case study :
Will student and sc Two tabular equivalent connections

Be careful :“student s” Is for student The name of the table is , In order to make it more convenient to go to the equivalent condition later , In the equivalence condition , Because both tables contain sno, therefore , In order to distinguish the two, we should add the table name in front of it .
take student and sc Non equivalent connection in the table

3. Internal and external connections
Internal connection (inner join) Display the multi table connection results that meet the conditions , Empty lines that are all empty will not be displayed ;
By default, it is internal connection
External connection ( outer join) Display the multi table connection results that meet the conditions , All empty lines NULL It also shows .
Mysql External connection not supported OUTER JOIN
4. Left connection and right connection
LEFT JOIN( Left connection ): Get all the records in the left table , Even if the right table does not have a matching record .
Case study :

RIGHT JOIN( The right connection ): And LEFT JOIN contrary , Used to get all records in the right table , Even if the left table does not have a matching record .
Case study :

5. Self join
Connect your own table to your own table , The core : A table can be split into two identical tables

6. Group query
Grouping queries are done by adding aggregate functions to a with group by Grouping clause select Statement to achieve .
Use aggregate functions to query , Common aggregate functions are as follows :
count([ DISTINCT | ALL] ): The number of records in the statistical table ;
count([ DISTINCT | ALL] < Name >): The number of values in a column of the statistical data table ;
max([ DISTINCT | ALL] < Name >): Find the maximum value of a column of values in the data table ;
min([ DISTINCT | ALL] < Name >): Find the minimum value of a column of values in the data table ;
sum( DISTINCT | ALL] < Name >): Calculate the sum of the values in a column of the data table ;
avg( DISTINCT | ALL] < Name >): Calculate the average of the values in a column of the data table ;
group by The syntax format of the clause is :
group by Name [having < Conditional expression >]Case study :
stay sc In the table grade grouping , Inquire about grade Greater than 90 The data of

7. Subquery
1. Subqueries are enclosed in parentheses
Case study :
Query and “ Li Yong ” The name and Department of students in the same department

2.where Subquery in Clause , Subqueries can include grouping functions
Subqueries can be found in where,having,from,update Of set Use in
Grouping functions can be in select,having,group by Use in
边栏推荐
猜你喜欢

『每日一问』ReentrantLock加锁解锁

那天帮妹纸装了个数据库。。。就又帮她整理了篇快捷键

Week summary

保姆级Scanner类使用详解

Jspdf generates PDF files. There is a problem of incomplete files. Files are downloaded in the background, but not in the foreground

粗柳簸箕细柳斗,谁嫌爬虫男人丑 之 异步协程半秒扒光一本小说

cf #785(div2) C. Palindrome Basis

*7-2 CCF 2015-09-2 日期计算

uni-app如何获取位置信息(经纬度)

ActiveMQ -- dead letter queue
随机推荐
正奇边形可划分成多少区域
MySQL takes the query result as the data updated by update, and concatenates it after the original field data (Lej)
*6-1 CCF 2015-03-2 数字排序
【代码源】每日一题 国家铁路
office文件对应的Content-Type类型
最短路问题 Bellman-Ford(单源最短路径)(图解)
Detailed explanation of the use of nanny scanner class
【代码源】 每日一题 素数之欢(bfs)
Go基础3
Go foundation 3
Numpy - 数组array的构造
OverTheWire-Bandit
ActiveMQ -- dead letter queue
基本的网络知识
数据控制语言(DCL)
通过robocopy对文件/夹进行复制
@3-2 CCF 2020-12-2 期末预测之最佳阈值
多态和接口
jsPDF生成PDF文件,文件不全问题,后台进行文件下载,前台不下载
@4-1 CCF 2020-06-1 线性分类器