当前位置:网站首页>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 .
边栏推荐
- 《一周搞定模电》-二极管
- Behavior tree file description
- 【CVPR 2021】Intra-Inter Camera Similarity for Unsupervised Person Re-Identification (IICS++)
- Regular expression learning
- 全面解读!Golang中泛型的使用
- "One week's work on Analog Electronics" - power amplifier
- Self learning neural network sequence -- 2 perceptron
- Badge series 7: use of codacy
- The first techo day Tencent technology open day, 628
- Is it safe to dig up money and make new debts
猜你喜欢

Classified catalogue of high quality sci-tech periodicals in the field of computing

Nacos registry structure and the principle of massive service registration and concurrent read-write source code analysis

【CVPR 2021】Unsupervised Pre-training for Person Re-identification(UPT)

正则表达的学习

Merrill Lynch data tempoai is new!

The first techo day Tencent technology open day, 628

thinkphp5手动报错

51 single chip microcomputer ROM and ram

xsync同步脚本的创建及使用(以Debian10集群为例)

《一周搞定模电》—集成运算放大器
随机推荐
Modify coco evaluation index maxdets=[10,15,20]
【Open5GS】Open5GS安装配置
Yolov5 results Txt visualization
Jz2440--- using uboot burning program
欧冠比赛数据集(梅西不哭-离开巴萨也可能再创巅峰)
"One week's data collection" - logic gate
The shutter tabbar listener is called twice
正则表达的学习
install opencv-contrib-dev to use aruco code
【CVPR 2021】Intra-Inter Camera Similarity for Unsupervised Person Re-Identification (IICS++)
[pulsar learning] pulsar Architecture Principle
"One week's work on Analog Electronics" - diodes
PHP does not allow images to be uploaded together with data (no longer uploading images before uploading data)
Bbox format conversion (detectron2 function library)
Merrill Lynch data technology expert team | application of recommendation of relevant contents in group system data retrieval
Thinkphp5 using the composer installation plug-in prompts that the PHP version is too high
【Sensors 2021】Relation-Based Deep Attention Network with Hybrid Memory for One-Shot Person Re-Id
Behavior tree file description
《一周学习模电》-电容、三极管、场效应管
【CVPR 2021】 Lifelong Person Re-Identification via Adaptive Knowledge Accumulation