外连接(左右),内连接,交叉连接

Posted on 2020-03-26 10:27  橙子j  阅读(245)  评论(0)    收藏  举报

https://www.cnblogs.com/logon/p/3748020.html

 

select * from table1 left join table2 on table1.id=table2.id

 

两个表dg1和dg2

 

 

  • 外连接
left join,right join分别是left outer join,right outer join的简写
以上再加一个full join就是外连接。
left join:左表为基础表,key相同的行连接起来
               左表有右表无→右表null表示,左表无右表有→无显示
select dg1.id1, dg1.name, dg2.id2, dg2.code, dg2.flow1 form dg1 dg1 left outer join dg2 dg2 on dg1.id1=dg2.id2

 

right join:以此类推
 
 
full join:左表有右表无→右表null表示,左表无右表有→左表null表示

 

 

 
  • 内连接
join(inner join):把两个表key一样的抠出来

 

 

 
  • 交叉连接
cross join:表的行所有结合可能性show出来
 
select * from table1 cross join table2 where table1.id=table2.id  (注:cross join后加条件只能用where,不能用on)