首页 新闻 会员 周边

sql 简单的语句求助!!!!!!!

0
悬赏园豆:10 [待解决问题]
 求助 现在有 a b c d 四个表 
a 表有 a.id字段
 b c d 三个也有a.id字段,但不一定三个表都有同一个a.id,可能b表有 c d表没有
 
需求是 
如何查询出 a表的id 在b c d中都没有的a.id的数据
IDOER的主页 IDOER | 初学一级 | 园豆:19
提问于:2016-12-02 09:10
< >
分享
所有回答(8)
-1
select * from a where a.id not in(select A.a.id from a A,b B,c C,d D where A.a.id=B.a.id and A.a.id=C.a.id and A.a.id=D.a.id)

 

~扎克伯格 | 园豆:1923 (小虾三级) | 2016-12-02 09:19
-1

select a.id from a where a.id not in(select b.id from b union select c.id from c union select d.id from d)

Daniel Cai | 园豆:10424 (专家六级) | 2016-12-02 09:20
0

将b,c,d表的ID 并在一起,a和并集left join就行了,伪代码如下

select *
from a 
left join
(
b
unionl 
c
union l
d
) u
on a.id=u.id
where u.id is null

 

悦光阴 | 园豆:2251 (老鸟四级) | 2016-12-02 09:25
-1

 光阴的方法可行。

s_p | 园豆:138 (初学一级) | 2016-12-02 11:57
0

SELECT *

FROM a

WHERE a.id NOT IN (

  SELECT B.id FROM b B

  UNION

  SELECT C.id FROM c C

  UNION

  SELECT D.id FROM d D

)

PS:UNION表示并集,NOT IN 后面,括号内的SQL语句表示,b,c,d三个表中所有a.id的并集。

 

n9d | 园豆:317 (菜鸟二级) | 2016-12-15 19:34
0

select * from a where id not in(select id from b union select id from c union select id from d)

 

sql 可以直接用

Merger | 园豆:225 (菜鸟二级) | 2017-04-12 10:17
0

select * from b where not exists (select 1 from (select id from a where a.id=b.id))

select * from c where not exists (select 1 from (select id from a where a.id=c.id))

select * from d where not exists (select 1 from (select id from a where a.id=d.id)) 

找到三个表里面 A表里面都不存在的id 然后把这些ID整合 找出相同的 出来 就能查询出来

   a表的id 在b c d中都不存在的a.id的数据

夜里挑键戳灯 | 园豆:299 (菜鸟二级) | 2017-06-22 11:58
0

select a.id from a where a.id not in(

select distinct e.id from (select b.id from b union select c.id from c union select d.id from d ) as e)

lqc1 | 园豆:152 (初学一级) | 2017-10-27 14:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册