使用最接近now()的时间字段更新数据库条目

时间:2013-04-13 11:45:57

标签: mysql

我有MySQL数据库表,其结构如下所述。

uid         info           time
6           null           2013-04-12 23:00:00
5           something      2013-04-12 22:00:00
4           something      2013-04-10 09:00:00
3           something      2013-04-09 19:00:00
2           something      2013-04-02 10:00:00
1           something      2013-04-01 11:00:00

如何在一个MySQL查询中更新具有最新时间值的行?

可能的解决方案之一是

SELECT uid FROM my_table ORDER BY time DESC LIMIT 1
UPDATE my_table SET info = 'something' WHERE uid = 'uid_received_in_previuos_query'

但它是两个连续的查询解决方案......

4 个答案:

答案 0 :(得分:0)

如果我做对了,你正在寻找MySQL NOW() function

用法示例:

UPDATE my_table SET info='something', time=NOW() WHERE id=6

或者,如果你正在寻找最新的udpate行,你可以

UPDATE my_table SET info='something', time=NOW() ORDER BY time DESC LIMIT 1

答案 1 :(得分:0)

您可以按照以下方式执行此操作

WHERE time > '2013-04-12 00:00:01' and time < '2013-04-12 23:59:59'

动态日期

WHERE time > date ("Y-m-d").'00:00:01' and time < date ("Y-m-d").'23:59:59'

答案 2 :(得分:0)

这应该有效

update  tableName first  inner join (SELECT uid FROM tableName ORDER BY time DESC LIMIT 1) as second SET first.info = 'something' where first.uid = second.uid

答案 3 :(得分:0)

这就是我要搜索的内容

 UPDATE my_table SET info = 'something' ORDER BY time DESC LIMIT 1;