dongsi4815 2015-02-18 06:08
浏览 72
已采纳

给定SELECT语句,我想通过多个子查询得到结果RECIPE_ID?

I have ingredient.ingredientId is 709,710,711 this id i am put in ingredient Sub-Query and AS match_percentage i am wright (3*100 / count is counting of ingredient.ingredientId , that all comes from my PHP code here i am giving this example for my solution.

Now result like those RECIPE_ID that i am pass three ingredient.ingredientId are match with this table recipe_ingredient.ingredientId and keep recipe_ingredient.recipeId

I want RECIPE_ID | 1 and 3 on result . ONLY WHO has that three ids 709,710,711 not even that two 709,710.

Here is my RECIPE_ID query:

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 

My Query Link : http://sqlfiddle.com/#!2/f4983/2

  • 写回答

1条回答 默认 最新

  • dongyu5482 2015-02-18 06:30
    关注

    Is this what you want? Please Check it with other scenario.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题