函数TimeStampDiff()是MySQL本身提供的可以计算两个时间间隔的函数,
语法为:TIMESTAMPDIFF(
unit
,datetime_expr1
,datetime_expr2
),其中unit单位有如下几种,
分别是:
FRAC_SECOND (microseconds),SECOND
,MINUTE
, HOUR
, DAY
, WEEK
, MONTH
, QUARTER
, or YEAR。
查找两个相邻时间之间的差值大于5分钟的SEQ_NO,SQL语句如下:
SELECT t.seq_no, T1.collect_date preDt, T.collect_date, TIMESTAMPDIFF(MINUTE,T1.collect_date, T.collect_date) mi
FROM realtime_data_1 T LEFT JOIN realtime_data_1 T1 ON T1.seq_no=T.seq_no-1
where TIMESTAMPDIFF(MINUTE,T1.collect_date, T.collect_date) > 5
and T.collect_date>'2014-11-29 20:00:00'
ORDER BY T.SEQ_NO DESC;