N
N
nailpaw2020-02-18 03:48:23
MySQL
nailpaw, 2020-02-18 03:48:23

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

2 answer(s)
V
v3shin, 2020-02-18
@nailpaw

SELECT * FROM news ORDER BY date DESC, id DESC LIMIT 5

D
Dmitry Kim, 2020-02-18
@kimono

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 question

Ask a Question

731 491 924 answers to any question