3

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

   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 个结果,并且 trats 是所有结果的总和,而不仅仅是那个特定的 RecipeID

4

1 回答 1

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;
于 2012-08-03T09:14:45.957 回答