关联查询join

表数据

select * from base_dept;

 

关联

#inner join 全部匹配的, 没有空值

select * from base_dept as t1 inner join base_dept as t2 ont1.id=t2.`parent_dept`; 

#left join 左边表匹配向右表,匹配到显示两表数据,匹配不到左表数据+右表显示为空

select * from base_dept as t1 left join base_dept as t2 ont1.id=t2.`parent_dept`; 

#只显示左表匹配不到右表的数据

select * from base_dept as t1 left join base_dept as t2 ont1.id=t2.`parent_dept` where t2.id is null; 

#right join 右边表匹配左边表,匹配到显示两表数据,匹配不到左表显示为空+右表数据.

select * from base_dept as t1 right join base_dept as t2 ont1.id=t2.`parent_dept`;

#只显示表右表匹配不到左表的数据

select * from base_dept as t1 right join base_dept as t2 ont1.id=t2.`parent_dept` where t1.id is null;

# union 联合 =left join+union+right join

select * from base_dept as t1 left join base_dept as t2 ont1.id=t2.`parent_dept` union select * from base_dept as t1 right join base_deptas t2 on t1.id=t2.`parent_dept`;

#两个表关联不上的

(select * from base_dept as t1 left join base_dept as t2 ont1.id=t2.`parent_dept`  where t2.id is null)union (select * from base_dept as t1 right join base_dept as t2 ont1.id=t2.`parent_dept` where t1.id is null);

# union all

select * from base_dept as t1 left join base_dept as t2 ont1.id=t2.`parent_dept` union all select * from base_dept as t1 right joinbase_dept as t2 on t1.id=t2.`parent_dept`;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值