我正在尝试使用我的查询来查找与animal.wild没有任何关系的特定Worker.wname为真。
例如从下表中我需要返回" Yossi"因为他没有使用wild = t
的行wname | type | wild
-------+-----------+------
David | Capricorn | f
David | goat | f
David | Lamb | f
David | Tiger | t
David | wolf | t
David | Wolf | t
Yossi | Capricorn | f
Yossi | goat | f
这是我到目前为止的查询(返回上面的表):
SELECT
worker.wname, animal.type, animal.wild
FROM resposibility
LEFT JOIN worker ON resposibility.wid = worker.wid
LEFT JOIN cage ON resposibility.cno = cage.cno
LEFT JOIN animal ON cage.cno = animal.cno
GROUP BY worker.wname, animal.wild, animal.type
ORDER BY worker.wname
答案 0 :(得分:3)
SELECT worker.wname
FROM resposibility
LEFT JOIN worker ON resposibility.wid = worker.wid
LEFT JOIN cage ON resposibility.cno = cage.cno
LEFT JOIN animal ON cage.cno = animal.cno
GROUP BY worker.wname
HAVING sum(case when animal.wild = 't' then 1 else 0 end) = 0
ORDER BY worker.wname
答案 1 :(得分:0)
您只需要表worker
,最简单的方法是使用NOT IN
运算符:
SELECT DISTINCT wname
FROM resposibility
WHERE wname NOT IN (SELECT wname FROM resposibility WHERE wild='t')
ORDER BY wname
答案 2 :(得分:0)
您还可以使用bool_or,如下所示:
SELECT worker.wname
FROM resposibility
LEFT JOIN worker ON resposibility.wid = worker.wid
LEFT JOIN cage ON resposibility.cno = cage.cno
LEFT JOIN animal ON cage.cno = animal.cno
GROUP BY worker.wname
having bool_or(animal.type) is false
ORDER BY worker.wname