The problem I first had with the following query was that the group by
clause was performed before the order by
:
The saved.recipe_id
column is an integer generated by UNIX_TIMESTAMP()
SELECT
saved.recipe_id,
saved.`date`,
user.user_id
FROM saved
JOIN user
ON user.id = saved.user_id
GROUP BY saved.recipe_id
ORDER BY saved.`date` DESC
So I tried all sorts of different possible solution with sub queries and other bs. In the end I ended up with trying out some different sub queries in the join
clause witch required me to change the table order from the from
clause to the join
clause. I decided to just try the following out:
SELECT
saved.recipe_id,
saved.`date`,
user.user_id
FROM user
JOIN saved
ON user.id = saved.user_id
GROUP BY saved.recipe_id
ORDER BY saved.`date` DESC
For some reason this seems to order correctly, but why?
How can this change make my query sort more correctly then before?
Does it really? or is it just happen to do it for the test cases I put it up against?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…