当前位置:网站首页>[MySQL] multi table connection query
[MySQL] multi table connection query
2022-06-28 04:08:00 【The wind blows tonight】
In a database with a huge amount of data , In order to play the role of the database , We have developed a variety of database operating languages to query data , Multi table join query is a common module .
Multi table join query , seeing the name of a thing one thinks of its function , It is to summarize and query the information data of multiple tables , To get the data we want .
Next, let's learn the common query methods .
Cartesian collection :
Cartesian sets simply associate tables when querying data , The data to m*n A combination of forms , Similar to matrix multiplication , It causes a lot of repeated and illogical data , Lost the original intention of filtering data .
The reason for Descartes set :
* Generating conditions :
1. Omit connection condition
2. Connection condition invalid
3. All rows in all tables are connected to each other
* Solution :
Add valid filter criteria
Internal connection :
* SQL92 grammar :
SELECT Query list
FROM Table name 1 Alias 1 , Table name 2 Alias 2
WHERE Connection condition
AND filter
GROUP BY Group list
HAVING Filter criteria after grouping
ORDER BY Sort list
* SQL99 grammar :
select Field list
from Table name 1
[inner] join Table name 2 on Conditions
where filter
group by Grouping conditions
having Screening criteria after grouping
order by Sort field
* Be careful :
1. Use the table name prefix to distinguish the same columns in multiple tables
2. Columns with the same column name in different tables can be distinguished by the alias of the table
3. If a table alias is used , It's in select Statement needs to use table alias instead of table name
4. Table aliases support up to 32 Character length , But the less advice the better
External connection ( Commonly used ):
The left outer join
* grammar :
select Field list
from surface 1
left [outer] join surface 2 on Conditions
...
* Be careful :
The left outer join queries all the data in the left table and its intersection , surface 1 Query main table for
-------------------------------------------------------------------------
5. Right connection
* grammar :
select Field list
from surface 1
right [outer] join surface 2 on Conditions
...
* Be careful :
The right outer join queries all the data in the right table and its intersection , surface 2 Query main table for
Subquery : Nested query in query , A nested query is called a subquery
Simply put, it is to split the complex query requirements , The split query results or contents are in brackets .
characteristic :
1、 Subqueries are enclosed in parentheses
2、 Subqueries can be placed in from Back 、select Back 、where Back 、having Back , But it's usually on the right side of the condition
3、 Subquery takes precedence over main query execution , The main query uses the execution result of the subquery
4、 Sub queries are divided into the following two categories according to the number of rows in the query results :
① Single line sub query
The result set has only one line
Generally used with single line operators :> < = <> >= <=
Illegal use of subqueries :
a、 The result of the subquery is a set of values
b、 The result of the subquery is empty
② Multi line sub query
How many lines does the result set have
Usually used with multiline operators :any、all、in、not in
in: Belong to any one of the sub query results
any and all You can often use other queries instead of
Paging query : seeing the name of a thing one thinks of its function , It is to adjust the number of pages in the display of query results .
* grammar :
select Field | expression ,...
from surface
where Conditions
group by Grouping field
having Conditions
order by Sorted fields
limit Starting entry index , Number of entries ;
* Example : Each page shows 3 Bar record
* SELECT * FROM student LIMIT 0,3; -- The first 1 page
* SELECT * FROM student LIMIT 3,3; -- The first 2 page
* SELECT * FROM student LIMIT 6,3; -- The first 3 page
* characteristic :
1. Starting entry index from 0 Start
2.limit Clause at the end of the query statement
3. The formula :select * from surface limit (page-1)*sizePerPage,sizePerPage
If :
Number of entries per page sizePerPage
Number of pages to display page
The joint query :
* grammar :
select Field | Constant | expression | function from surface where Conditions union 【all】
select Field | Constant | expression | function from surface where Conditions union 【all】
select Field | Constant | expression | function from surface where Conditions union 【all】
.....
select Field | Constant | expression | function from surface where Conditions
* characteristic :
1、 The number of columns in multiple query statements must be consistent
2、 Multiple query statements query almost the same type of columns
3、union It means to go heavy ,union all It means not to weigh
A little thought : How not to use max Function query data , Take the maximum wage as an example :
Method 1 : Use sorting and paging queries :
SELECT employee_id,salary
FROM employees
ORDER BY salary DESC
LIMIT 1;Method 2 : Subquery
SELECT
employee_id,salary
FROM
employees e
WHERE
e.salary NOT IN (
SELECT DISTINCT
a.salary
FROM
employees a
JOIN employees b ON a.salary < b.salary
)
边栏推荐
猜你喜欢

Arrangement of basic electrical knowledge (II)

利用 telegraf influxdb grafana 搭建服务器监控平台

Staggered and permutation combination formula

GenICam GenTL 标准 ver1.5(2)

Are the two flame retardant standards of European furniture en 597-1 and en 597-2 the same?

iptables防火墙规则和firewalld防火墙规则详解

Particle theory of light (photoelectric effect / Compton effect)

猫狗队列的问题

Does the applet image component not display pictures?

How to apply for ASTM E108 flame retardant test for photovoltaic panels?
随机推荐
Staggered and permutation combination formula
Detailed explanation of KVM common commands
Does the applet image component not display pictures?
@Several scenarios of transactional failure
05 mongodb summary of various column operations
English语法_形容词/副词3级-比较级_常用短语
sqlserver 数据库之事物使用入门 案例
leetcode:494.数组中添加加减运算符得到指定值的所有方法
One article tells you what kubernetes is
等保三级密码复杂度是多少?多久更换一次?
指针链表
关于 SY8120I 的DC-DC的降压芯片的学习(12V降至3.3V)
leetcode:单调栈结构(进阶)
一千行 MySQL 学习笔记,值得收藏!
Zipkin 服务链路追踪
Adder - Notes
MSC 307(88) (2010 FTPC Code)第2部分烟气和毒性测试
MSC 307(88) (2010 FTPC Code) Part 9床上用品试验
内卷、躺平与中年危机的相关思考
[graduation season] graduate summary