的方式,我會去解決這個就是嘗試計算每個配方,你需要成分的數量,並加入與原料本身的數量,如果兩個號碼匹配,你有一個候選配方。
因此,要獲得配方需要的配料數量,您必須執行類似操作(這更像是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
現在你加入前2個查詢來獲得你有足夠成分idRecieps,並與配方表加入他們拿到配方名稱。
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語法。
它的工作!謝謝! –