![](/img/trans.png)
[英]How Do I JOIN a third Table called all_recipes using recipe_id KEY
[英]Given SELECT statement , I want result RECIPE_ID by multiple subquery ?
我有ingredient
。 ingredientId
是709710711這個號我把配料子查詢我和AS match_percentage
我是萊特(3*100 / count
計數的ingredient
。 ingredientId
,所有來自我的PHP代碼在這里我給這個例子我的解決方案。
現在導致像那些RECIPE_ID
我是通過三項ingredient
。 ingredientId
是匹配與此表recipe_ingredient
。 ingredientId
並保持recipe_ingredient
。 recipeId
我想要RECIPE_ID
| 結果為1和3。 只有WHO擁有三個ID 709,710,711,甚至沒有兩個709,710。
這是我的RECIPE_ID
查詢:
SELECT
`recipe`.`recipeId` AS recipe_id ,
(select count(`recipe_ingredient`.ingredientId) from `recipe_ingredient` where `recipe`.recipeId = `recipe_ingredient`.recipeId) as ingredientCount ,
IF(
(select (3*100 / count(DISTINCT `recipe_ingredient`.ingredientId)) from `recipe_ingredient` where `recipe_ingredient`.recipeId = `recipe`.recipeId)>99
,100
,round( (select (3*100 / count(DISTINCT `recipe_ingredient`.ingredientId)) from `recipe_ingredient` where `recipe_ingredient`.recipeId = `recipe`.recipeId) )
)
as match_percentage ,
GROUP_CONCAT(
DISTINCT `recipe_ingredient`.ingredientId
ORDER BY `recipe_ingredient`.ingredientId ASC
) as recipeIngredients
from `recipe`
left join `recipe_ingredient` on `recipe_ingredient`.recipeId = `recipe`.recipeId
left join `ingredient` on `ingredient`.ingredientId = `recipe_ingredient`.ingredientId
where `recipe`.`recipeId` IN(
SELECT
`recipe_ingredient`.`recipeId`
FROM `recipe_ingredient`
WHERE `recipe_ingredient`.`ingredientId`
IN(
SELECT `ingredient`.`ingredientId` AS linkIng
FROM `ingredient`
WHERE `ingredient`.`ingredientId` IN(709,710,711) or `ingredient`.`linkIngredientPerent` IN(709,710,711)
)
GROUP BY `recipe_ingredient`.`recipeId`
ORDER BY `recipe_ingredient`.`recipeId` ASC
)
and (select (3*100 / count(DISTINCT `recipe_ingredient`.ingredientId)) from `recipe_ingredient` where `recipe_ingredient`.recipeId = `recipe`.recipeId) > 24
group by `recipe`.recipeId
我的查詢鏈接: http : //sqlfiddle.com/#!2/f4983/2
這是你想要的嗎? 請在其他情況下進行檢查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.