SQL - 查找所有者没有租车的记录。 我有以下内容(在MS Access中):
Car_Owner
+----+------+---------+--------+
| id | name | Car reg | Rental |
+----+------+---------+--------+
| 1 | Adam | ABC123 | True |
+----+------+---------+--------+
| 2 | John | XXX111 | True |
+----+------+---------+--------+
| 2 | John | XXX222 | False | //Same person as above
+----+------+---------+--------+
| 3 | Eva | YYY999 | False |
+----+------+---------+--------+
如何找出谁租用任何车?在这种情况下,只有伊娃。约翰不应该被列入,因为他已经有租车。
非常感谢任何帮助。
答案 0 :(得分:4)
一种方法使用聚合:
select name
from car_owner
group by name
having sum(iif(rental, 1, 0)) = 0;
您还可以通过执行以下操作来检查该值是否为false:
having max(rental) = 0 and min(rental) = 0
答案 1 :(得分:1)
Select * from Car_Owner
Where Rental = False
and Id Not In
(Select Id from Car_Owner Where Rental = True)
答案 2 :(得分:1)
select * from car_owner
where name not in (select name from car_owner where rental = True);
答案 3 :(得分:1)
SQL下面也可以帮助你:
SELECT *
FROM Car_Owner
WHERE Rental = False
AND id NOT IN ( SELECT id
FROM Car_Owner
WHERE Rental = True )
OR
SELECT *
FROM Car_Owner a
WHERE Rental = False
AND NOT EXISTS ( SELECT id
FROM Car_Owner b
WHERE b.Rental = True
AND b.id = a.id )