【发布时间】:2020-12-09 05:40:06
【问题描述】:
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
【问题讨论】:
标签: sql sqlite count subquery relational-division