MySQL - 从一组几个可能的时间戳中选择最近的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11812215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL - Select most recent date out of a set of several possible timestamps?
提问by Craig van Tonder
I would like to know if this is possible using a mysql query:
我想知道这是否可以使用 mysql 查询:
I have a table called updates.
我有一个叫做更新的表。
In the updates table I have 2 columns, id
and timestamp
.
在更新表中,我有 2 列,id
和timestamp
.
I have 5 rows each with the same id
but with different date timestamps. An example of this timestamp would be the value: 2012-08-04 23:14:09
.
我有 5 行,每行都有相同id
但不同的日期时间戳。这个时间戳的一个例子是值:2012-08-04 23:14:09
。
I would like to select only the most recent timestamp value out of the 5 results. This could also be explained by saying that I would like to select the value that is closest to the current date and time. How could I do this?
我只想从 5 个结果中选择最新的时间戳值。这也可以解释为我想选择最接近当前日期和时间的值。我怎么能这样做?
回答by Vitor M
SELECT id, timestamp FROM updates ORDER BY timestamp DESC LIMIT 1
回答by Julien
have you tried SELECT MAX(timestamp)
?
你试过SELECT MAX(timestamp)
吗?