饮料数据库设计
问题描述:
我想设计一个饮料数据库,需要一些设计帮助。饮料数据库设计
我创建了以下表格:列出所有可能成分(即朗姆酒,可乐,伏特加和橙汁)的成分表。饮料桌上有所有可能饮料的清单(即“朗姆酒&可乐”,“螺丝刀”)。最后,Recipes表格描述了如何制作饮料,因此对于“朗姆酒&可乐”,有两种成分,一种是朗姆酒的成分ID,一种是可口可乐的成分ID,两种饮料的外号都是相同的饮料。
我想要做的是给出一个可用的成分id - 1(朗姆酒),2(可乐)和3(伏特加)的列表,我想拿出一个可能的饮料列表。所以我可以做朗姆酒&可乐,但不是螺丝刀,因为我没有橙汁。
有关如何编写这样的select语句或如何更好地设计表的任何建议?
感谢
答
假如你有3个表:Drinks
,Ingredients
和Recipes
,这里是一个Select语句,这将使你只Drinks
,他们的Recipes
有所有您给出Ingredients
:
Select
d.*
From
Drinks d
Where
not exists
(
select 1
from Recipes r
where r.DrinkId = d.Id and r.IngredientId in (1,2,3)
)
答
SELECT *
FROM (
SELECT coktail_id,count(ingredient_id) as nb_ingredient
FROM coktail_ingredient
WHERE ingredient_id IN (1,2,3) --where 1 is rhum , 2 is vodka and 3 is coke
group by coktail_id
)
where nb_ingredient = 3--3 for the number of ingredient you need
是我使用的技术。
您是否找到想要的答案? – everton 2011-12-29 14:21:27