2016-05-28 108 views
-1
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; 
+2

你有什麼到目前爲止已經試過,爲什麼不能你谷歌嗎?大量關於此的文檔。 –

+0

我已經嘗試了一切,我花了很多小時Google搜索它......問題是我想要一列顯示所有成分,另一個顯示多少匹配從兩個表...我一直無法顯示它作爲一個結果..我已經嘗試加入表聯盟各種各樣。我真的很感謝一些幫助儘快謝謝@LukePark –

+0

輸出列是'recipe_id,recipeFound,recipeCount'?對於每個配方你想顯示數量和發現? – Arulkumar

回答

2

根據您的意見我所瞭解的是您需要顯示三列爲recipeId, foundIngredients, countIngredients

由於與出表模式我以爲結構,並得出以下查詢:

SQL小提琴是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 
+0

謝謝配合完美!......是否有可能在同一查詢中計算找到和計數之間的差異..並使用recipe_ingredients加入第三個表recipe_id –

相關問題