简体   繁体   中英

Whats wrong with this SQL query

This query is giving me error so whats wrong with this query?

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

I think that the position of group by in query is not correct.

should be like that, where then group by , then order

      WHERE schedule.day = 'saturday' 
     GROUP BY recipeId

    ORDER BY catId ASC

You are thinking correct. GROUP BY should come after WHERE clause and before ORDER BY clause.

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

Here without aggregate function you can't use group by . and group by condition is after where clause and before order by clause.

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

just replace order of

GROUP BY recipeId

after

WHERE schedule.day = 'saturday'

it need to be..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM