作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Sqlite3
我如何选择只作为宠物而不是食物的动物? POF 是“宠物或食物”栏。一个动物可以同时属于这两个群体。这是实际问题的较小版本。我不想把它分成更多的表。
animal pof
----------
fish pet
fish food
pig food
cat pet
dog pet
horse pet
mouse pet
duck pet
duck food
cow food
rabbit pet
rabbit food
gerbil pet
worm <null>
chicken food
我有以下内容,但似乎很尴尬:
SELECT * from
(SELECT NAME, POF, count(*) as cnt
FROM ANIMALS
GROUP BY NAME) AS GC
WHERE GC.cnt == 1 AND GC.POF == 'pet'
正确屈服:
NAME POF cnt
---------------
cat pet 1
dog pet 1
gerbil pet 1
horse pet 1
mouse pet 1
最佳答案
使用 NOT IN
排除所有具有 pof = 'food'
的动物:
select *
from animals
where pof = 'pet'
and animal not in (select animal from animals where pof = 'food')
或者,如果您只想要列
animal
您可以使用
EXCEPT
:
select animal from animals where pof = 'pet'
except
select animal from animals where pof = 'food'
见
demo .
关于sql - 如何选择属于一组但不属于另一组的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63495686/
我是一名优秀的程序员,十分优秀!