SQL查询帮助 - 查找没有关系的行

时间:2010-06-07 14:55:28

标签: sql

考虑一个带有Client表和Book表的数据库:

客户:person_id

书籍:book_id

Client_Books:person_id,book_id

你怎么能找到所有没有书籍的人ID? (不进行外连接并寻找空值)

4 个答案:

答案 0 :(得分:3)

select * 
from Client  as c
where not exists(select * from Client_Books where person_id =c.person_id ) 

答案 1 :(得分:2)

select *
from Client 
where person_id not in (select person_id from Client_Books)

答案 2 :(得分:0)

SELECT * FROM Client WHERE person_id not in (SELECT person_id FROM Client_Books)

答案 3 :(得分:0)

select *  
from Client as c 
where (select coun(*) from Client_Books where person_id =c.person_id ) = 0

COUNT表示完整性,因为已经发布了EXISTS和IN解决方案。