时间戳字段是DATETIME
列,格式为2012-03-19 00:23:14
。如何比较两行时间戳并找到较大的时间戳?我正在使用的下面的查询无效。
UPDATE report 1 status = 'time is larger' WHERE EXISTS
(SELECT ip_src, layer4_sport, timestamp FROM
(SELECT ip_src, layer4_sport, timestamp from report 1) AS tmpb
WHERE report 1.layer4_sport = tmpb.layer4_sport
AND report 1.ip_src = tmpb.ip_src
AND report 1.timestamp > tmpb.timestamp
GROUP BY ip_src, layer4_sport, timestamp HAVING COUNT(*) = 2)
答案 0 :(得分:7)
您可以尝试使用if语句来验证A时间戳是否大于B时间戳。
select if(UNIX_TIMESTAMP('2009-02-01 00:00:00') > UNIX_TIMESTAMP('2009-01-01 00:00:00'), true, false)
答案 1 :(得分:1)
尝试使用UNIX_TIMESTAMP()
,UNIX_TIMESTAMP(date)
函数来比较两个时间戳。
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
答案 2 :(得分:1)
感谢上帝,现在查询还可以。
缺少的是表名后的SET语法,并且EXISTS中也应省略GROUP BY语法。
UPDATE report 1 SET status = 'time is larger' WHERE EXISTS
(SELECT ip_src, layer4_sport, timestamp FROM
(SELECT ip_src, layer4_sport, timestamp from report 1) AS tmpb
WHERE report 1.layer4_sport = tmpb.layer4_sport
AND report 1.ip_src = tmpb.ip_src
AND report 1.timestamp > tmpb.timestamp)