0

I'm trying to construct a MySQL-query, where the result is based on the given ingredients.

If said that I provide Tequila, Orangejuice and Grenadine, I want the result to be Tequila Sunrise ONLY. Not everything that has either ingredient in them.

My current query is:

SELECT * 
FROM drink 
INNER JOIN mix 
ON drink.id = mix.drinkid 
WHERE Ing IN ('Tequila', 'Apelsinjuice', 'Grenadine') 

I would be so grateful for any help!

Thank you in advance!

4

1 回答 1

2

这是“set-within-sets”查询的一个示例。我更喜欢使用聚合和having子句来执行此操作,因为这是最灵活的方法:

SELECT d.* 
FROM drink INNER JOIN mix 
     ON drink.id = mix.drinkid 
group by drink.id
having sum(Ing = 'Tequila') > 0 and
       sum(Ing = 'Apelsinjuice') > 0 and
       sum(Ing = 'Grenadine') > 0

子句中的每个都sum()having计算给定成分的行数。> 0确保成分存在。and坚持所有三种成分都在配方中。

于 2013-06-08T23:15:39.170 回答