簡體   English   中英

給定SELECT語句,我想要多個子查詢的結果RECIPE_ID嗎?

[英]Given SELECT statement , I want result RECIPE_ID by multiple subquery ?

我有ingredient ingredientId是709710711這個號我把配料子查詢我和AS match_percentage我是萊特(3*100 / count計數的ingredientingredientId ,所有來自我的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

這是你想要的嗎? 請在其他情況下進行檢查。

http://sqlfiddle.com/#!2/f4983/8

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM