mysql三表关联查询

条件: a,b,c三张表,a表里面有b,c表的主键

三张表,需要得到的数据是标红色部分的。sql如下: 

方法一: 内连接

select a.uid,a.uname,a.upsw,a.urealname,a.utel,a.uremark, b.rid,b.rname,b.rremark,c.deptid,c.deptname,c.deptremark

from table1 a,table2 b,table3 c 
where a.sems_role_rid=b.rid 
and a.udeptid=c.deptid ;

方法二:左连接

select a.uid,a.uname,a.upsw,a.urealname,a.utel,a.uremark, b.rid,b.rname,b.rremark,c.deptid,c.deptname,c.deptremark
from table1 a 
left join table2 b on  a.sems_role_rid=b.rid 
left join table3 c on a.udeptid=c.deptid ;

LEFT JOIN 可以实现统一数据库多表联合查询符合条件的数据。 

更多查询参考:https://blog.csdn.net/weixin_42576112/article/details/80899313

Mysql 多表连接查询参考:https://blog.csdn.net/qq_35723367/article/details/80233040

猜你喜欢

转载自blog.csdn.net/weixin_42724467/article/details/88707693