join&Union

Original link: http://www.cnblogs.com/enhance/p/10980459.html
id name id name
1 zhangsan 1 hanmeimei
2 lysis 2 wangwu
3 wangwu    

 

 

join:select * from A join B on A.name=B.name -----取交集

id name id name
1 wangwu 2 wangwu

full/out join:select * from A full join B on A.name=B.name  ----取并集

id name id name
1 zhangsan null null
2 lysis null null
3 wangwu 2 wangwu
null null 1 hanmeimei

 

left join:select * from A left join B on A.name=B.name

id name id name
1 zhangsan null null
2 lysis null null
3 wangwu  2 wangwu

right join: select * from A right join B on B.name

id name id name
null null 1 hanmeimei
3 wangwu 2 wangwu

 

union: select name from A union select name from B (union and in the union all select statement queries require the same column)

name
zhangsan
lysis
wangwu
hanmeimei

union all: the difference is more union union will merge duplicate values, but does not merge union all

    select name from A union all select name from B

name
zhangsan
lysis
wangwu
hanmeimei
wangwu 

Reproduced in: https: //www.cnblogs.com/enhance/p/10980459.html

Guess you like

Origin blog.csdn.net/weixin_30391339/article/details/94834140
Recommended