当前位置:网站首页>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 .
边栏推荐
- Detectron2 save (according to maxap50) model during training_ best. PTH weight
- 挖财打新债安全吗
- "One week's data collection" - logic gate
- 使用递归或while循环获取父/子层级结构的名称
- Classified catalogue of high quality sci-tech periodicals in the field of computing
- Jz2440 - - - utiliser le programme de gravure uboot
- Self taught machine learning series - 1 basic framework of machine learning
- Thinkphp5 manual error reporting
- jz2440---使用uboot燒錄程序
- Jz2440--- using uboot burning program
猜你喜欢

Edge computing is the sinking and extension of cloud computing capabilities to the edge and user sides

online trajectory generation

Behavior tree file description

工企专利匹配数据(数十万数据量)1998-2014年

How to compile builds

jz2440---使用uboot燒錄程序

【pulsar学习】pulsar架构原理

《單片機原理及應用》——概述

【CVPR 2021】 Lifelong Person Re-Identification via Adaptive Knowledge Accumulation

Badge series 4: use of circle Ci
随机推荐
Optimization of power assisted performance of QPM suspended window
"One week's work on Analog Electronics" - power amplifier
"One week's work on Analog Electronics" - integrated operational amplifier
【CVPR 2021】 Lifelong Person Re-Identification via Adaptive Knowledge Accumulation
PHP extracts TXT text to store the domain name in JSON data
Explained: A Style-Based Generator Architecture for GANs (StyleGAN)
Board end power hardware debugging bug
Construction practice of bank intelligent analysis and decision-making platform
51 single chip microcomputer ROM and ram
Introduction to QPM
Js--- get the data with the same key value in the object array to get a new array
Champions League data set (Messi doesn't cry - leaving Barcelona may reach another peak)
計算領域高質量科技期刊分級目錄
安装 新版本cmake & swig & tinyspline
Merrill Lynch data tempoai is new!
Click the mask layer to close the pop-up window
Kubernetes cluster deployment (v1.23.5)
Industrial and enterprise patent matching data (hundreds of thousands of data) 1998-2014
Merrill Lynch data technology expert team | building a cloud native product system based on containers
The most complete and simple nanny tutorial: deep learning environment configuration anaconda+pychart+cuda+cudnn+tensorflow+pytorch