Mysql,加入查询,困惑

时间:2012-08-03 09:12:27

标签: mysql

我面临的问题是,我想加入这两个问题。

   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

1 个答案:

答案 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;