[英]Ignore multiple records in LEFT JOIN query by case
I am trying to use my query to find the specific Worker.wname that has none of his relations with the animal.wild is TRUE. 我正在尝试使用我的查询来查找与animal.wild没有任何关系的特定Worker.wname为TRUE。
for example from the following table I need to return "Yossi" since he has no row with wild = t 例如,从下表中我需要返回“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
This the query i've got so far (which returns the TABLE above): 这是我到目前为止的查询(返回上面的表):
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
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
You only need table worker
and the simplest is to use the NOT IN
operator: 您只需要表
worker
,最简单的是使用NOT IN
运算符:
SELECT DISTINCT wname
FROM resposibility
WHERE wname NOT IN (SELECT wname FROM resposibility WHERE wild='t')
ORDER BY wname
You could also use the bool_or as follow: 你也可以使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.