开发者

re-sorting the output of a SELECT LIMIT query

开发者 https://www.devze.com 2023-04-03 00:21 出处:网络
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:

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消