簡體   English   中英

如何將這些查詢合並為一個結果

[英]How to combine these queries together as one result

SELECT COUNT(recipe_id) AS Found
FROM recipe_ingredients R, users_ingredients U
WHERE R.key_ingredient = U.key_ingredient;

SELECT recipe_id, COUNT(recipe_id) As Count FROM recipe_ingredients GROUP BY recipe_id;

根據您的評論,我理解您需要顯示三列作為recipeId, foundIngredients, countIngredients

由於沒有表模式,因此我假定了結構並派生以下查詢:

SQL Fiddle是http://sqlfiddle.com/#!9/eee151/3

SELECT COU.recipe_id, IFNULL(FOU.Found, 0) AS Found, COU.Count
FROM  ( SELECT recipe_id, COUNT(recipe_id) As Count 
        FROM recipe_ingredients 
        GROUP BY recipe_id) COU
LEFT OUTER JOIN ( SELECT R.recipe_id, COUNT(R.key_ingredient) AS Found
        FROM users_ingredients U
        JOIN recipe_ingredients R ON R.key_ingredient = U.key_ingredient
        GROUP BY R.recipe_id) FOU ON FOU.recipe_id = COU.recipe_id

暫無
暫無

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

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