Mysql Ordering Query
Hello I want to run a query to get the last five records from a table, but in reverse order. Currently I have:
$query = "SELECT * FROM Table ORDER BY id DESC LIMIT 5";
which isn't quite what I want.
For example if the last five records are
15 16 17 18 19
I want them returned as
15 16 17 18 19
Not 19 18 17 16 15 which is what the above does.
How do开发者_开发问答 I achieve this? If I change DESC to ASC it gives 1 2 3 4 5 so that doesn't work either.
Try a sub query:
SELECT *
FROM (SELECT * FROM Table ORDER BY id DESC LIMIT 5) AS tmp
ORDER BY id ASC
You can use a sub-select to do that:
SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 5) AS t ORDER BY id
SELECT * FROM (SELECT * FROM Table ORDER BY id DESC LIMIT 5) T1 ORDER BY id ASC;
精彩评论