我面临的问题是,我想加入这两个问题。
SELECT recipes.RecipeID ,recipes.Name,recipes.ImageThumb,recipes.RatingTotal,
count(ratings.RecipeID) AS trates
FROM recipes
JOIN ratings
ON ratings.RecipeID = recipes.RecipeID
WHERE recipes.Name LIKE '%ade%'
ORDER BY Name;
此查询无法正常工作,当有多个结果时,它会带来1个结果,并且trates将作为所有结果的总和,而不仅仅是针对特定的RecipeID
答案 0 :(得分:3)
我认为这就是你要找的东西:
SELECT recipes.RecipeID ,recipes.Name,recipes.ImageThumb,recipes.RatingTotal,
COUNT(ratings.RecipeID) AS trates
FROM recipes
LEFT JOIN ratings
ON ratings.RecipeID = recipes.RecipeID
WHERE recipes.Name LIKE '%ade%'
GROUP BY RecipeID
ORDER BY Name;