当前位置:网站首页>Mysql database easy learning 09 - commonly used by data analysts: multi table query of data query language DQL

Mysql database easy learning 09 - commonly used by data analysts: multi table query of data query language DQL

2022-06-22 01:58:00 Smart Aries

1 The relationship between tables

1.1 one-on-one

 Insert picture description here

1.2 One to many

 Insert picture description here

1.3 Many to many

 Insert picture description here

2 How to connect

2.1 Transverse connection

2.1.1 Internal connection inner join

Connect two tables according to the connection conditions , Return the line that meets the condition
 Insert picture description here

select  Field 1[,] from  surface 1[ inner] join  surface 2 on  Connection condition ; 

2.1.2 Left connection left join

In addition to the lines that satisfy the join conditions, the result , It also includes all the rows of the left table
 Insert picture description here

select  Field 1[,] from  surface 1 left join  surface 2 on  Connection condition ; 

2.1.3 The right connection right join

In addition to the lines that satisfy the join conditions, the result , It also includes all the rows of the right table
 Insert picture description here

select  Field 1[,] from  surface 1 right join  surface 2 on  Connection condition ; 
2.1.4 The cartesian product cross join

Suppose the set A={a,b}, aggregate B={1,2,3}, Then the Cartesian product of two sets is {(a,1),(a,2),(a,3),(b,1),(b,2),(b,3)}

select  Field 1[,] from  surface 1, surface 2[,]; 
select  Field 1[,] from  surface 1  cross join  surface 2[,]; 		

Eliminate Cartesian product
Line by line judgment , Equal leave , Unequal exclusion

select  Field 1[,] from  surface 1, surface 2[,] where  filter ; 

2.1.5 Self join

By setting the table alias , Virtualize the same table into multiple tables for connection

select * from tabel a inner join table b
on a.id = b.id

2.2 Longitudinal connection

2.2.1 Concept

Put more than one select The query results of the statement are combined into a result set
Of the merged result set Number of columns 、 Sequence and data type It has to be exactly the same

2.2.2 union duplicate removal :
#select  Field 1[, Field 2,…] from  Table name  union select  Field 1[, Field 2,…] from  Table name ; 
select * from t1 union select * from t2;

2.2.3 union all No weight removal :

#select  Field 1[, Field 2,…] from  Table name  union all select  Field 1[, Field 2,…] from  Table name ; 
select * from t1 union all select * from t2;

3 Connection condition

3.1 Equivalent connection

 surface 1.id= surface 2.id

3.2 Unequal value connection

 surface 1.id  Compare   surface 2.id
原网站

版权声明
本文为[Smart Aries]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220134336684.html