re-sorting the output of a SELECT LIMIT query
Fellow coders, i have a table that contains a number of 开发者_开发技巧rows each with a date column. I would like to select the last 6 most recent rows. I can do that like this:
SELECT *
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
This returns the rows I need but they are returned in DESC date order. What I want is the last 6 rows in ASC date order. How can I re-sort the output of the SELECT? Any ideas?
thanks
SELECT *
FROM (
SELECT *
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
) s
ORDER BY s.StatsDate
Surround the query in an outer query and order that in a different order.
SELECT * FROM
(
SELECT *
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
) s
ORDER BY `StatsDate` ASC
SELECT *
FROM (
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
) as t
ORDER BY t.`StatsDate` ASC;
精彩评论