查询关联不上的数据,三张表查询

select * from s_temp_import2;(表a)

select * from s_temp_import;(表b)

select * from s_temp_import_d;(表c)

(1) 内连接查询:

select a.* from s_temp_import2 a,s_temp_import b,s_temp_import_d c
where a.pro_no=b.pro_no and a.pro_d_no=c.pro_d_no; (注:此处都是通过a表的值进行关联)

(2) b表中包含a.pro_no 但c表中不包含a.pro_d_no;c表中包含a.pro_d_no但b表中不包含a.pro_no 的查询语句:

select a.* from u_md_dm.s_temp_import2 a
where 1=1 and (not exists (select 1 from u_md_dm.s_temp_import where a.pro_no=pro_no) or not exists(select 1 from u_md_dm.s_temp_import_d where a.pro_d_no=pro_d_no ));

 (3)a表和c表左连接查询,a做主表

select a.pro_no,a.pro_d_no as a_pro_d_no,c.pro_d_no as c_pro_d_no,c.pro_d_name from u_md_dm.s_temp_import2 a left join u_md_dm.s_temp_import_d c on a.pro_d_no=c.pro_d_no;(注:left join 是 left outer join 的缩写)

原文地址:https://www.cnblogs.com/BruceDu/p/7280978.html