当前位置:网站首页>Several connection query methods of SQL (internal connection, external connection, full connection and joint query)
Several connection query methods of SQL (internal connection, external connection, full connection and joint query)
2022-06-26 09:28:00 【Great white bear_ BlankBear】
Preparation form
drop table if exists test_a;
CREATE TABLE `test_a` (
`id` varchar(10) NOT NULL,
`username` varchar(10) NOT NULL,
`password` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
drop table if exists test_a_description;
CREATE TABLE `test_a_description` (
`id` varchar(10) NOT NULL,
`age` varchar(10) ,
`address` varchar(50) ,
`parent_id` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;Prepare the data
insert into test_a values('1',' Xiao Ming ','11');
insert into test_a values('2',' Ning Ning ','22');
insert into test_a values('3',' Sensitive ','33');
insert into test_a values('6',' To live ','66');
insert into test_a_description values('1','10','aaa','1');
insert into test_a_description values('2','20','bbb','2');
insert into test_a_description values('3','30','ccc','3');
insert into test_a_description values('4','40','ddd','4');One 、 Internal connection (inner join)

Typical join operations , Use something like = or <> And so on ). Including equal join and natural join .
Inner joins use comparison operators to match rows in two tables based on the values of columns that are common to each table . for example , retrieval students and courses All rows with the same table ID .
What we found is the intersection of two tables , We can only find out if we have both tables
SQL:
select * from surface A inner join surface B on Judge the condition ;
select * from surface A, surface B where Judge the condition ;
select a.*, ad.* from test_a as a inner join test_a_description as ad on a.id=ad.parent_id;
select a.*, ad.* from test_a as a, test_a_description as ad where a.id=ad.parent_id;result :

Two 、 External connection
1、 The left outer join (left join)

The left table is the main table ( Query all ), The right table is the auxiliary table ( No display null)
SQL:
select * from surface A left join surface B on Judge the condition ;
select a.*, ad.* from test_a as a left join test_a_description as ad on a.id=ad.parent_id;result :

2、 Right connection (right join)
The right table is the main table ( Query all ), The left table is the auxiliary table ( No display null)
SQL:
select * from surface A right join surface B on Judge the condition ;
select a.*, ad.* from test_a as a right join test_a_description as ad on a.id=ad.parent_id;
result :

3、 ... and 、 Full connection (full join)

All the data of the two tables are displayed
SQL:
select * from surface A full join surface B on Judge the condition ;
select a.*, ad.* from test_a as a full join test_a_description as ad on a.id=ad.parent_id;
result :

Four 、 union ( Merge ) Inquire about (union)
MySQL Don't recognize FULL join, So you can go through union To achieve
SQL:
select a.*, ad.* from test_a as a left join test_a_description as ad on a.id=ad.parent_id
union
select a.*, ad.* from test_a as a right join test_a_description as ad on a.id=ad.parent_id;
result :

5、 ... and Intersection
What we need to register is that we have another one “ Intersection ” cross join, such Join There's no way to express it in a diagram , Because it's a watch A And table B Data for a N*M The combination of , Cartesian product . The expression is as follows :
SELECT * FROM TableA CROSS JOIN TableB
This Cartesian product will produce 4 x 4 = 16 Bar record , Generally speaking , We seldom use this grammar . But we have to be careful , If you don't use nested select sentence , In general, the system will generate Cartesian product and then filter it . This is very dangerous for performance , Especially when the watch is big .
6、 ... and 、 Differential analysis
External connection : External connection not only returns data rows that meet the connection and query criteria , Also return some unqualified lines . External connections fall into three categories : The left outer join (LEFT OUTER JOIN)、 Right connection (RIGHT OUTER JOIN) And all outside connection (FULL OUTER JOIN)
What the three have in common is that they all return qualified connection conditions and query conditions ( namely : Internal connection ) The data line . The differences are as follows :
The left outer join : It also returns the data rows in the left table that do not meet the query criteria of the connection condition sheet .
Right connection : It also returns the data rows in the right table that do not meet the query criteria of the connection condition sheet .
Full outer join : It also returns the data rows in the left table that do not meet the query criteria of the connection condition sheet , It also returns the data rows in the right table that do not meet the query conditions of the connection condition sheet . Total outer connection is actually a mathematical collection of upper left outer connection and right outer connection ( Remove the repetition ), namely “ Total external = Left lateral UNION Right outside ”.
explain : The left watch is “(LEFT OUTER JOIN)” The table on the left of the keyword . Of course, the right watch is the one on the right . In three types of external connections ,OUTER Keywords can be omitted .
7、 ... and 、SQL The basic principle of query
Single table query : according to WHERE Condition filter the records in the table , Form the middle table ( The middle table is invisible to users ); And then according to SELECT Select the corresponding column to return the final result .
Two tables join query : The product of two tables ( The cartesian product ) And use ON Conditions and join types are filtered to form an intermediate table ; And then according to WHERE Conditionally filter the records in the middle table , And according to SELECT The specified column returns the query result .
Multi table join query : First, query the first table and the second table according to the connection of the two tables , Then use the query result and the third table to do the join query , And so on , Until all the tables are connected , Finally, an intermediate result table is formed , And then according to WHERE Conditionally filter the records in the middle table , And according to SELECT The specified column returns the query result .
understand SQL The process of query is SQL The theoretical basis of optimization .
边栏推荐
- Collection object replication
- 《一周搞定模电》—负反馈
- 《一周搞定数电》-逻辑门
- QPM suspended window setting information
- Js--- get the data with the same key value in the object array to get a new array
- 计算领域高质量科技期刊分级目录
- Statistics of various target quantities of annotations (XML annotation format)
- Param in the paper
- Self taught neural network series - 4 learning of neural network
- Self learning neural network sequence -- 2 perceptron
猜你喜欢

Pycharm occasionally encounters low disk space

【C】 Frog jumping steps and Hanoi Tower problem (recursion)

Badge series 7: use of codacy

"One week's work on Analog Electronics" - optocoupler and other components

online trajectory generation

Jz2440--- using uboot burning program

"One week's data collection" - logic gate

【AAAI 2021】Few-Shot One-Class Classification via Meta-Learning 【FSOCC via Meta-learning】

2021-11-29 轨迹规划五次多项式

常用电路设计
随机推荐
【Sensors 2021】Relation-Based Deep Attention Network with Hybrid Memory for One-Shot Person Re-Id
Self taught programming series - 2 file path and text reading and writing
Self taught neural network series - 4 learning of neural network
Execution process at runtime
Self taught neural network series - 3. First knowledge of neural network
install opencv-contrib-dev to use aruco code
Merrill Lynch data technology expert team | building a cloud native product system based on containers
Badge series 4: use of circle Ci
Solutions for safety management and control at the operation site
《一周搞定数电》-逻辑门
Runtimeerror: object has no attribute NMS error record when using detectron2
install ompl.sh
【CVPR 2021】 Lifelong Person Re-Identification via Adaptive Knowledge Accumulation
Catalogue gradué de revues scientifiques et technologiques de haute qualité dans le domaine de l'informatique
Router bridging settings
How to solve the problem that NVIDIA model cannot be viewed by inputting NVIDIA SMI and quickly view NVIDIA model information of computer graphics card
Detectron2 save (according to maxap50) model during training_ best. PTH weight
Badge series 7: use of codacy
《一周搞定模电》—基本放大电路
Nacos registry structure and the principle of massive service registration and concurrent read-write source code analysis