当前位置:网站首页>MySQL multi table query
MySQL multi table query
2022-07-19 03:06:00 【Tatakai!!!】
List of articles
Mysql Multi-table query
The foundation of multi table query is based on Cartesian product !, To understand the execution process of Cartesian product !
- The drive table is scanned only once , The driven table may be queried many times
One 、 Internal connection
Internal connection : Find the intersection of two tables ( Common )
select * from surface 1 t, Table two c where t.id = ci.d;( Implicit inner join )
select * from surface 1 t inner join Table two c on t.id = ci.d;( Implicit inner join )
select * from surface 1 t join Table two c on t.id = ci.d;( Explicit inner connection )
- In connection ,【 The driver table 】 It is automatically selected after system optimization , Will implement the plan 【 Less scanning 】 Table selection of 【 The driver table 】
Two 、 External connection
The difference between internal and external connections :
- about 【 Internal connection 】 Two watches in , if 【 The driver table 】 The record in 【 Was the driver table 】 No matching record found in , Then the record will not be added to the final result set .
- about 【 External connection 】 Two watches in , Even if 【 The driver table 】 The record in 【 Was the driver table 】 No matching record found in , Also add this record to the final result set , For different 【 Drive table selection 】, External connections can also be divided into 【 The left outer join 】 and 【 Right connection 】.
Left lateral
The left table is the driver table :
select * from Table 1 t left join Table two c on t.id = c.id;
Right outside
The right table is the drive table :
select * from Table 1 t right join Table two c on t.id = c.id;
3、 ... and 、 Full connection
Although my MySQL This method is not supported , It can be solved in other ways , as follows .
SELECT * from teacher t right outer join course c on c.t_id = t.id
union
SELECT * from teacher t left outer join course c on c.t_id = t.id
In this result , Whether it's the left table or the right table , All the data is included in the result set :( duplicate removal )

Four 、 Subquery
- Environmental preparation :
DROP TABLE IF EXISTS emp;
DROP TABLE IF EXISTS dept;
# Create department table
CREATE TABLE dept(
did INT PRIMARY KEY AUTO_INCREMENT,
dname VARCHAR(20)
);
# Create an employee table
CREATE TABLE emp (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(10),
gender CHAR(1), -- Gender
salary DOUBLE, -- Wages
join_date DATE, -- Date of entry
dep_id INT,
FOREIGN KEY (dep_id) REFERENCES dept(did) -- Foreign keys , Related department table ( The primary key of the Department table )
);
-- Add Department data
INSERT INTO dept (dNAME) VALUES (' R & D department '),(' The Marketing Department '),(' Finance Department '),(' The sales department ');
-- Add employee data
INSERT INTO emp(NAME,gender,salary,join_date,dep_id) VALUES
(' The Monkey King ',' male ',7200,'2013-02-24',1),
(' Pig eight quit ',' male ',3600,'2010-12-02',2),
(' Tang's monk ',' male ',9000,'2008-08-08',2),
(' Bones jing ',' Woman ',5000,'2015-10-07',3),
(' Spider essence ',' Woman ',4500,'2011-03-14',1),
(' Small white dragon ',' male ',2500,'2011-02-14',null);
- Concept
Nested query in query , A nested query is called a subquery .
What is nested query in query ? Let's take an example :
demand : Query the information of employees whose salary is higher than Zhu Bajie .
To fulfill this requirement , We can achieve... In two steps , First step : Find out first Pig Bajie's salary
select salary from emp where name = ' Pig eight quit '
The second step : Query the information of employees whose salary is higher than Zhu Bajie
select * from emp where salary > 3600;
In the second step 3600 You can go through the first step sql Find out , So will 3600 Use the first step sql Statement to replace
select * from emp where salary > (select salary from emp where name = ' Pig eight quit ');
This is the nested query statement in the query statement .
Sub queries differ according to the query results , The effect is different
- The result of subquery statement is single row and single column , Subquery statement As a condition value , Use = != > < Wait for conditional judgment
- The result of subquery statement is multiple rows and single column , Subquery statement As a condition value , Use
inAnd other keywords for conditional judgment - The result of subquery statement is multi row and multi column , Subquery statement As a virtual table
** Case study :** Inquire about ‘ Finance Department ’ and ‘ The Marketing Department ’ All employee information
SELECT * from dept where dname = ' Finance Department ';
-- In fact, we only need Department id that will do , namely :
SELECT did from dept where dname = ' Finance Department ';
SELECT did from dept where dname = ' The Marketing Department ';
-- To sum up, two sentences :
SELECT did from dept where dname = ' Finance Department ' or dname = ' The Marketing Department ';
-- The result is multiple rows and single column !
Subquery statement : According to the subquery results id To nest
-- Inquire about ' Finance Department ' perhaps ' The Marketing Department ' The Department of all employees did
select did from dept where dname = ' Finance Department ' or dname = ' The Marketing Department ';
select * from emp where dep_id in (select did from dept where dname = ' Finance Department ' or dname = ' The Marketing Department ');
Case study : The date of employment inquiry is ‘2011-11-11’ After that, employee information and department information
-- The date of employment inquiry is '2011-11-11' Later employee information
select * from emp where join_date > '2011-11-11';
-- The query result is a multi row and multi column table !( Virtual table )
-- Connect and query the virtual table with the Department information table
select * from (select * from emp where join_date > '2011-11-11') t1 join dept on t1.dep_id = dept.did;
边栏推荐
- [MCU simulation] (IX) instruction system - add, ADDC, sub, Inc, Dec, Da of arithmetic operation instructions
- mysql复制表
- 2. Actual use of asynctool framework
- 05-中央处理器
- RESNET learning notes
- GraphQL初识
- [MCU simulation] (XVII) control transfer instructions - call and return instructions
- Code demonstration of fcos face detection model in openvino
- 重写equals为什么要重写hashcode
- [MCU simulation] (XX) org - set start address
猜你喜欢
随机推荐
RTX3090安装pytorch3D
[MCU simulation] (XXI) dB (define byte) - define byte
人脸关键点检测
内置键盘连续444
yolov6 学习初篇
【单片机仿真】(十四)指令系统位操作类指令 — 位数据传送指令MOV、位变量修改指令
LETV has more than 400 employees? Living a fairy life without a boss, the official responded
HCIA_ Nat experiment
乐视还有400多位员工?过着没有老板的神仙日子 官方出来回应了...
重写equals为什么要重写hashcode
[single chip microcomputer simulation] (XII) instruction system logic operation instruction - logic XOR instruction XRL, accumulator clear 0 and reverse instruction
From the perspective of MySQL architecture, how does an SQL statement execute?
[MCU simulation] (VI) addressing mode - index addressing and relative addressing
MySQL storage engine details
Go language realizes sending SMS verification code and logging in
Built in keyboard continuous 444
Letv a plus de 400 employés? Le jour de l'immortel sans patron, les autorités ont répondu...
Multi layer packet structure and TCP triple handshake
2022-07-16:以下go语言代码输出什么?A:[];B:[5];C:[5 0 0 0 0];D:[0 0 0 0 0]。 package main import ( “fmt“ )
快照:数据快照(数据兜底方式)





![[NoSQL] redis master-slave, sentinel, cluster](/img/69/37b80398617040984b006d3d7b71b9.png)



