下面的查询每次出现一个配方注释时都会产生一条记录。我想为每个配方ID带来一个记录,按配方的分组似乎不起作用。

SELECT recipecomments.RecipeID
      ,recipecomments.CommentID
      ,recipes.RecipeID
      ,recipes.Name
      ,recipes.CategoryID
      ,recipes.RatingTotal
      ,recipes.ImageMed
FROM recipecomments
     JOIN recipes ON recipecomments.RecipeID = recipes.RecipeID
ORDER BY recipecomments.CommentID

最佳答案

如果要按配方ID分组,则需要确定要显示的CommentID。
根据您的语法,我猜测是最高的。

 SELECT
      recipecomments.RecipeID,
      MAX(recipecomments.CommentID),
      recipes.RecipeID,
      recipes.Name,
      recipes.CategoryID,
      recipes.RatingTotal,
      recipes.ImageMed
 FROM recipecomments
                JOIN recipes ON recipecomments.RecipeID = recipes.RecipeID
 group by
      recipecomments.RecipeID,
      recipes.RecipeID,
      recipes.Name,
      recipes.CategoryID,
      recipes.RatingTotal,
      recipes.ImageMed

关于mysql - mysql查询分组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12038975/

10-10 06:35