1

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

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

4 回答 4

1

应该是这样, where 然后 group by ,然后 order

      WHERE schedule.day = 'saturday' 
     GROUP BY recipeId

    ORDER BY catId ASC
于 2013-08-03T11:44:36.300 回答
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
于 2013-08-03T11:44:54.933 回答
1

这里没有聚合函数你不能使用group by. andgroup by条件是 afterwhere子句和 beforeorder 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
于 2013-08-03T11:46:19.580 回答
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
于 2013-08-03T11:51:18.290 回答