标签 - join

sql join    2021-12-31 11:17:53    16    0    0

下图展示了 LEFT JOIN、RIGHT JOIN、INNER JOIN、OUTER JOIN 相关的 7 种用法

title

例子

  1. create table #a (af1 int)
  2. insert into #a select 1
  3. insert into #a select 2
  4. create table #b (bf1 int)
  5. insert into #b select 2
  6. insert into #b select 3
  7. select 'lwft join' x,a.*,b.* from #a a left join #b b on a.af1=b.bf1
  8. select 'right join' x,a.*,b.* from #a a right join #b b on a.af1=b.bf1
  9. select 'inner join' x,a.*,b.* from #a a inner join #b b on a.af1=b.bf1
  10. select 'lwft join b is null' x,a.*,b.* from #a a left join #b b on a.af1=b.bf1 where b.bf1 is null
  11. select 'right join a is null' x,a.*,b.* from #a a right join #b b on a.af1=b.bf1 where a.af1 is null
  12. select 'full outer join' x,a.*,b.* from #a a full outer join #b b on a.af1=b.bf1
  13. select 'full outer join a or b null' x,a.*,b.* from #a a full outer join #b b on a.af1=b.bf1 where a.af1 is null or b.bf1 is null
  14. drop table #a
  15. drop table #b