Mysql 如果另一个Select返回行,则选择
我正在尝试在你拥有的物品中选择烹饪食谱 我有一个名为IngreditSown的表,其结构如下:Mysql 如果另一个Select返回行,则选择,mysql,node.js,Mysql,Node.js,我正在尝试在你拥有的物品中选择烹饪食谱 我有一个名为IngreditSown的表,其结构如下: idType (int) amount (int) idRecipe (int) name (varchar) 另一个名为recipes的表具有此结构: idType (int) amount (int) idRecipe (int) name (varchar) 和另一个名为RecipeIngElements的表 我想展示一下你可以用你拥有的元素制作的食谱,我怎样才能做到这一点 我试图在一个
idType (int) amount (int)
idRecipe (int) name (varchar)
另一个名为recipes的表具有此结构:
idType (int) amount (int)
idRecipe (int) name (varchar)
和另一个名为RecipeIngElements的表
我想展示一下你可以用你拥有的元素制作的食谱,我怎样才能做到这一点
我试图在一个查询中实现它,因为我真的不知道如何在节点js上抛出和数组
谢谢SELECT*FROM recipes internal JOIN
从RecipeivingElements中选择idRecipe
其中RecipeIngElements.id输入
从ingredientsOwn中选择ingredientsOwn.idType
作为a.idRecipe=recipes.idRecipe上的
我的方法是,试着计算每个食谱,你需要的配料数量,并将其与自己的配料数量相加,如果两个数字匹配,你就有了一个候选食谱 因此,要获得配方所需的配料数量,您必须执行类似于sql server语法的操作,因此请尽量关注概念,而不是语法:
select idRecipe, count(*) as neededIngredientsCount
from recipeIngredients
group by idRecipe
要获得每个配方的可用配料数量,你必须将你的配料加入到其他配料中,才能知道每个配方有多少匹配的配料
select ingredientsOwn.idRecipe, count(*) as matchingIngredientsCount
from ingredientsOwn inner join recipeIngredients
on ingredientsOwn.idType = recipeIngredients.idType
where ingredientsOwn.amount >= recipeIngredients.amount
group by ingredientsOwn.idRecipe
现在,您将加入前两个查询,以获取您有足够成分的iDReciep,并将它们与recipes表联接以获取配方名称
select r.idRecipe, r.name from
((select idRecipe, count(*) as neededIngredientsCount
from recipeIngredients
group by idRecipe) as in
inner join
(select ingredientsOwn.idRecipe, count(*) as matchingIngredientsCount
from ingredientsOwn inner join recipeIngredients
on ingredientsOwn.idType = recipeIngredients.idType
where ingredientsOwn.amount >= recipeIngredients.amount
group by ingredientsOwn.idRecipe) as io
on in.idRecipe = io.idRecipe
and in.neededIngredientsCount = io.matchingIngredientsCount
inner join
(select * from recipes) as r
on r.idRecipe = in.idRecipe)
希望这能有所帮助,很抱歉不能提供有效的mysql语法。请您解释一下为什么这段代码会回答这个问题?代码唯一的答案是,因为他们不教解决方案。