sql 多数据表联合查询问题

来源:百度知道 编辑:UC知道 时间:2024/06/05 12:18:00
现在有四个数据表:
table1,table2,table3,table4
它们共有一个关键字段id
其中table1包含有其它三个数据表的数据,也有一部分是table2,table3,table4都没有的。
现在是要把table1在其它三个中都没有的数据行找出来!

select * from table1 where id not in (select id from table2 union select id from table3 union select id from table4)

select table1.*
from table left join table2 on table1.id=table2.id
left join table3 on table1.id=table2.id
left join table4 on table1.id=table2.id
where table2.id is null and table3.id is null and table4.id is null

select * from table1
where not exists (select * from table2 where table2.id = table1.id) and
not exists (select * from table3 where table3.id = table1.id) and
not exists (select * from table4 where table4.id = table1.id)

select * from table1 left join table2 on table1.id<>table2.id left join table3 on table1.id<>table3.id left join table4 on table1.id<>table4.id

1 楼正解。