假装我有一张cupcake_rating
桌子:
id | cupcake | delicious_rating
--------------------------------------------
1 | Strawberry | Super Delicious
2 | Strawberry | Mouth Heaven
3 | Blueberry | Godly
4 | Blueberry | Super Delicious
我想找到所有具有“超级美味”和“嘴巴天堂”评级的纸杯蛋糕。我觉得这很容易使用一个group by
子句和一个having
.
我刚在想:
select distinct(cupcake)
from cupcake_rating
group by cupcake
having delicious_rating in ('Super Delicious', 'Mouth Heaven')
我知道我不能有两个单独的 AND 语句。我能够使用以下方法实现我的目标:
select distinct(cupcake)
from cupcake_rating
where cupcake in ( select cupcake
from cupcake_rating
where delicious_rating = 'Super Delicious' )
and cupcake in ( select cupcake
from cupcake_rating
where delicious_rating = 'Mouth Heaven' )
这不会令人满意,因为一旦我添加了我正在寻找的第三种类型的评级,查询将需要几个小时(有很多纸杯蛋糕评级)。