这个SQL查询有什么问题

时间:2013-08-03 11:43:36

标签: mysql sql

这个查询给了我错误,所以这个查询有什么问题?

SELECT recipes.recipeId as recipeId,
       recipes.title as title, 
       recipes.cookingtime as cookingtime, 
       recipes.serving as serving, 
       schedule.catId as catId, 
       images.imagePath AS imagePath 
  FROM recipes 
 INNER JOIN schedule ON recipes.recipeId = schedule.recipeId 
 INNER JOIN images   ON images.recipeId = recipes.recipeId 
 GROUP BY recipeId
 WHERE schedule.day = 'saturday'  
 ORDER BY catId ASC

我认为查询中group by的位置不正确。

4 个答案:

答案 0 :(得分:1)

应该是那样的,然后分组,然后命令

      WHERE schedule.day = 'saturday' 
     GROUP BY recipeId

    ORDER BY catId ASC

答案 1 :(得分:1)

你的想法是正确的。 GROUP BY应该在WHERE子句之后和ORDER BY子句之前。

SELECT recipes.recipeId as recipeId,
       recipes.title as title, 
       recipes.cookingtime as cookingtime, 
       recipes.serving as serving, 
       schedule.catId as catId, 
       images.imagePath AS imagePath 
  FROM recipes 
 INNER JOIN schedule ON recipes.recipeId = schedule.recipeId 
 INNER JOIN images   ON images.recipeId = recipes.recipeId 
 WHERE schedule.day = 'saturday'  
 GROUP BY recipeId
 ORDER BY catId ASC

答案 2 :(得分:1)

此处没有聚合功能,您无法使用group bygroup by条件在where子句之后和order by子句之前。

SELECT recipes.recipeId as recipeId,
           recipes.title as title, 
           recipes.cookingtime as cookingtime, 
           recipes.serving as serving, 
           schedule.catId as catId, 
           images.imagePath AS imagePath 

    FROM 
           recipes INNER JOIN schedule 
                              ON recipes.recipeId = schedule.recipeId 

                                        INNER JOIN images 
                                                   ON images.recipeId = recipes.recipeId 

 --   GROUP BY recipeId

    WHERE schedule.day = 'saturday'  ORDER BY catId ASC

答案 3 :(得分:0)

只需替换

的顺序
GROUP BY recipeId

之后

WHERE schedule.day = 'saturday'

它需要......

SELECT recipes.recipeId as recipeId,
       recipes.title as title, 
       recipes.cookingtime as cookingtime, 
       recipes.serving as serving, 
       schedule.catId as catId, 
       images.imagePath AS imagePath 
 FROM recipes 
 INNER JOIN schedule ON recipes.recipeId = schedule.recipeId 
 INNER JOIN images   ON images.recipeId = recipes.recipeId 
 WHERE schedule.day = 'saturday'  
 GROUP BY recipeId
 ORDER BY catId ASC