sql三表查询找出没有写入中间表的记录

来源:百度知道 编辑:UC知道 时间:2024/09/18 04:16:37
我有3个表A,B,C.
A表示用户表 B表是中间表,C表是意向表.
A表内容是id,name C表内容是id,name B表内容是a.id,c.id.
我需要的是 找出A表中没有在B表中和C表进行关联的A表id
其实我需要的查询语句和C表没什么关系 C表里的内容是已经固定的了 不会发生变化 A表会随时写入新的记录 但有时没有注意没有和C表进行关联,也就是把A表id和C表id都写入B表中 我想要的查询语句就是找出A表中没有写入B表的id

A表在B,C表中都没有进行关联的ID
select id from A
where id not exisets (select id from (select id from B union select id from C) test);

A表没有在B,C表中同时进行关联的ID
select id from A
where id not exisets (select B.id from B inner join C on B.id=C.id);

补充:根据你的补充的补充
select id from A whre id not in (select id from B);
或者
select id from A whre id not exists (select id from B);

C表进行关联的A表id :
select t1.name,t2.name from c t1
inner join a t2 on t2.id =t1.id

A表中没有在B表中:
select * from b t1
inner join a t2 on t2.id =t1.id