[英]SQL(Left Join) Get all recipes which have all their ingredients in another table
I need to get all recipes where all the ingredients are in another table and I don't know how I could to this.我需要得到所有配料都在另一张桌子上的所有食谱,我不知道我怎么能做到这一点。
'Ingredients' table (from the recipes): “成分”表(来自食谱):
'UserIngredients' table (from the user): 'UserIngredients' 表(来自用户):
Ingredients table (of the recipes):配料表(食谱): User ingredients table:用户成分表:
Now I just want to get recipe id 13 back because this recipe just needs 'Butter' and 'Mehl'.现在我只想找回 13 号食谱,因为这个食谱只需要“黄油”和“梅尔”。 What I don't want to get are the recipes which also need 'Butter' and 'Mehl' but also something else, because that's not in the user ingredients table.我不想得到的食谱也需要“Butter”和“Mehl”,但也需要其他东西,因为它们不在用户成分表中。
Now I want a query(I think with an Inner Join) to get the recipes(ids) which have all ingredients in the other table.现在我想要一个查询(我认为使用内部连接)来获取另一个表中包含所有成分的食谱(id)。 I hope you understand my problem and can help me.我希望你能理解我的问题并能帮助我。 Big Thank You!非常感谢!
You need a LEFT
join of ingredients
to useringredients
and then group by recipeid
:您需要将ingredients
LEFT
连接到useringredients
,然后group by recipeid
:
select i.recipeid
from ingredients i left join useringredients ui
on ui.ingredient = i.ingredient
group by i.recipeid
having count(*) = count(ui.ingredient)
The condition having count(*) = count(ui.ingredient)
makes sure that all ingredients and no more are used for that recipeid
having count(*) = count(ui.ingredient)
确保所有成分和不再用于该recipeid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.