Reputation: 125
I need to get last 3 records of a table and put them in ascending order.
The following query will return last 3 records of table, but it won't sort them.
select column_1, column_2 from table_name order by id desc limit 3
Is there a way to sort these records also, without using a subquery?
I tried
select column_1, column_2 from table_name order by id desc, id asc limit 3
but it didn't work.
Upvotes: 2
Views: 371
Reputation:
In my view I don't think that any other way without sub query will make it easily
Try below :
SELECT a.* from
(SELECT column_1, column_2 FROM table_name ORDER BY id DESC LIMIT 3) as a
ORDER BY a.id ASC
subquery is not a bad idea in this situation where you using it with limit
.
Upvotes: 4
Reputation: 6389
If you want to use PDO:
$stmt = $dbh->prepare('SELECT * FROM your-table ORDER BY date DESC Limit 3');
Upvotes: -1