0

我有 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

4 回答 4

0

如果我猜对了,您正在寻找MySQL NOW() 函数

使用示例:

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

或者,如果您正在寻找最新的行来更新任何内容,您可以

UPDATE my_table SET info='something', time=NOW() ORDER BY time DESC LIMIT 1
于 2013-04-13T11:48:55.410 回答
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'
于 2013-04-13T11:49:21.157 回答
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
于 2013-04-13T11:54:32.923 回答
0

这就是我一直在寻找的

 UPDATE my_table SET info = 'something' ORDER BY time DESC LIMIT 1;
于 2013-04-13T12:14:11.713 回答