mysql多表查询

select * from 表1 INNER JOIN 表2 on 表1.关联字段=表2.关联字段; 普通内连接
select * from 表1,表2 where 表1.关联字段1=表2.关联字段2 ;隐藏内连接
select * from 表1 left JOIN 表2 on 表1.关联字段=表2.关联字段;左连接
select * from 表1 right JOIN 表2 on 表1.关联字段=表2.关联字段;右连接
select * from 表1 left JOIN 表2 on 表1.关联字段=表2.关联字段 右表字段 is null ;左独有数据
select * from 表1 right JOIN 表2 on 表1.关联字段=表2.关联字段 where 左表字段 is null;右独有数据
(1)内连接+左独有+右独有

select * from dept INNER JOIN emp on dept.dept1=emp.dept2
UNION
select * from dept left JOIN emp on dept.dept1=emp.dept2 where name is null
UNION
select * from dept RIGHT JOIN emp on dept.dept1=emp.dept2 where dept_name is null;

(2)左连接+右独有

select * from dept left JOIN emp on dept.dept1=emp.dept2
UNION
select * from dept RIGHT JOIN emp on dept.dept1=emp.dept2 where dept_name is null;

(3)右连接+左独有

select * from dept right JOIN emp on dept.dept1=emp.dept2
UNION
select * from dept left JOIN emp on dept.dept1=emp.dept2 where name is null;

posted @ 2025-11-18 17:15  黑田慎平  阅读(2)  评论(0)    收藏  举报