在sqlite中有像“ only”这样的关键字吗?我必须写一个查询来显示只保留红船的水手姓名。所以我当前的查询是:
select s.sname
from sailor s, boat b, reservation r
where s.sname = r.sname
and b.bname = r.bname
and b.color = 'red';
上面查询的问题是它还会显示保留红色+其他颜色小船的水手姓名。我的查询结果:
a reserve red boat
a reserve green boat
b reserve red boat
但由于他只保留红船,因此应该只显示b。
最佳答案
您可以使用NOT EXISTS
子句仅用red
船来过滤水手。
select r.sname
from reservation r
join boat b
on b.bname = r.bname
and b.color = 'red'
and not exists ( select 1 from reservation r2
join boat b2
on r2.bname = b2.bname
and r2.sname = r.sname
and b2.color <> 'red' )
关于sql - 仅在哪里查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26771232/