Reputation: 686
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.
Upvotes: 2
Views: 84
Reputation: 8451
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
Upvotes: 0
Reputation: 32602
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
Upvotes: 1
Reputation: 1247
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
Upvotes: 1
Reputation: 37233
should be like that, where then group by , then order
WHERE schedule.day = 'saturday'
GROUP BY recipeId
ORDER BY catId ASC
Upvotes: 1