具有类似数组支持的数据库?

时间:2014-11-23 20:09:56

标签: mysql sql database

所以我有一些信息要存储,让我们把它愚蠢到食物来创造一个例子。 配方含有一种或多种成分,每种成分也含有一定量。

所以配方(名为recipe的表中的一行)可以包含: id : 0 name : something disgusting ingredientid1 : 0 ingredientamount1 : 20 ingredientid2 : 1 ingredientamount2: 6 ..etc

到目前为止我只有mysql的经验,但我愿意学习。本质上,我可以将成分序列化为字符串并存储该字符串,然后使用LIKE运算符查找具有特定成分的所有食谱......但我会对此类解决方案感到厌恶。

目标是让最终用户能够为服务器提供一系列成分,以获得返回的食谱列表,这些食谱可以使用这些成分制作。

如果不使用LIKE运算符,我该怎么做呢?

1 个答案:

答案 0 :(得分:4)

您应该将其作为单独的表实现:

Recipe('id', 'name'...)
Ingredients('id', 'recipe_id', 'name', 'amount')

然后,您可以通过WHERE recipe_id = x

简单地获取所有成分

此外,如果您想与任何食谱分享成分。你应该使用树表,如:

Recipe('id', 'name')
Ingredients('id', 'name')
RecIngr('recipe_id', 'ingr_id', 'amount')

然后,您可以通过加入获得所有食谱成分:

select i.name, ri.amount From Ingredients i
inner join RecIngr ri on ri.ingr_id=i.id
where ri.recipe_id = x