MySQL选择按日期DESC排序的最后一行的每一天

时间:2012-12-19 08:05:16

标签: mysql

我有数据,

Stamp                dataField1 dataFiel2
2012-05-23 23:59:59  ....        ....
2012-05-23 23:50:00  ....        ....
2012-05-24 24:43:23  ....        ....
2012-05-24 24:12:10  ....        ....
2012-05-25 25:15:00  ....        ....
2012-05-26 25:53:59  ....        ....

我希望选择每一天的最后一行,结果应该看起来像

Stamp                dataField1 dataFiel2
2012-05-23 23:59:59  ....        ....
2012-05-24 24:43:23  ....        ....
2012-05-26 25:53:59  ....        ....

4 个答案:

答案 0 :(得分:0)

SELECT MAX(Stamp) as Last, other_column FROM table GROUP BY DAY(Stamp)

答案 1 :(得分:0)

SELECT Stamp, dataField1, dataFiel2  FROM tableName 
GROUP BY Stamp ORDER BY Stamp ASC

答案 2 :(得分:0)

这个怎么样?

SELECT MAX(Stamp) as Stamp, dataField1, dataField2
FROM tableName
GROUP BY DAYOFYEAR(Stamp)

我会说使用 DAYOFYEAR 而不是 {{每个人都说1}} 。有关差异,请参阅以下链接。

Demo

在此演示中,DAY提供3行 INCORRECT ,与使用DAY的4行相对。

答案 3 :(得分:0)

尝试此查询 -

SELECT
  Stamp, dataField1, dataField2, dataField3
FROM
  (SELECT * FROM table ORDER BY Stamp DESC) t
GROUP BY
  DATE(Stamp)