Trey Tyler
Trey Tyler

Reputation: 273

MYSQL SELECT last 5 results, then order those 5 by date?

I am building a "Comments" section where I would be grabbing the last 5 results in this table.

COMMENT_ID |     DATE       | COMMENT
....... BUNCH OF RESULTS HERE
111            2018-07-28     HELLO
112            2018-07-26     HEY
113            2018-07-25     HOW ARE YOU

The table is obviously, much larger. I want to grab the most recent 5 commens (ID's 113 to 109) so I have to use a ORDER BY COMMENT_ID DESC, LIMIT 5.

However, when I loop through the results I'd like to have the oldest comment of those 5 results show up first and the most recent last. I can't seem to do this with my current query as when I switch to COMMENT_ID ASC it shows comment ids 1-5.

Any ideas would be greatly appreciated.

Upvotes: 0

Views: 281

Answers (1)

0of1
0of1

Reputation: 376

You can do it with a subquery: select * from (select * from comments order by comment_id desc limit 5) t1 order by date

Not sure if you care about id or date as ordering...my query does a mix.

Upvotes: 1

Related Questions