3

时间戳字段是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)
4

3 回答 3

7

您可以尝试使用 if 语句来验证 A 时间戳是否大于 B 时间戳。

select if(UNIX_TIMESTAMP('2009-02-01 00:00:00') > UNIX_TIMESTAMP('2009-01-01 00:00:00'), true, false)
于 2012-04-30T13:47:58.087 回答
1

尝试 UNIX_TIMESTAMP(),UNIX_TIMESTAMP(date)函数来比较两个时间戳。

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp

于 2012-04-30T13:31:54.457 回答
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)
于 2012-05-01T04:22:04.533 回答