关联查询join
Posted zghw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关联查询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`;
以上是关于关联查询join的主要内容,如果未能解决你的问题,请参考以下文章