mysql查询组由

时间:2012-08-20 13:51:43

标签: mysql

以下查询为每次recipecomments.RecipeID出现时都会记录一条记录,我只想为每个recipeID带一条记录,按recipeid分组似乎不起作用

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

1 个答案:

答案 0 :(得分:1)

如果您按recipeid进行分组,则需要确定要显示的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