Answer the question
In order to leave comments, you need to log in
How to sort output in mysql multiple times in one query?
I need to get the last 5 records by date, query:
SELECT * FROM news ORDER BY date DESC LIMIT 5
The resulting output needs to be sorted in reverse order by id. Since the news stores the type date 2020-02-18, if I use the query above, I get this output: News1, News2, News3.... these news have the same date. And I need to get them in reverse order.
How can I do this with 1 request or can there be another solution?
For example, there is a solution to use the date time type, then everything is solved with 1 request, but this is not my case.
Answer the question
In order to leave comments, you need to log in
SELECT t1.* FROM news t1
LEFT JOIN (SELECT id FROM news t2 ORDER BY created_at DESC LIMIT 5) t3 ON t1.id = t3.id
ORDER BY t1.id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question