简体   繁体   中英

How to get upper and lower data where time difference is more than 1 hour in mysql..?

I have a table having per hour table records like this :

在此处输入图片说明

As you can see every record have 1 hour difference but the highlighted one has more than one hour. I need a query which should return me both values upper ( id 11379728 ) and lower ( id 11378448 ) where gap is more than 1 hour.

Any help will be appreciated.

Note :- please ignore last datetime column as it serves different purpose.

I got a working solution (perfect in my case) :

SET @lastDate := (SELECT dateColumn FROM table ORDER BY dateColumn ASC LIMIT 1);

SELECT @lastDate as lastDate, HOUR(TIMEDIFF(dateColumn, @lastDate)) as diff, @lastDate := dateColumn, dateColumn
FROM table 
group by dateColumn 
having diff > 1
order by dateColumn ASC;

In some cases mysql 5.5 and 5.6 will give different results for HAVING clause. Above query will run good on mysql 5.5. I have created another version to run on both version similarly like this :

SET @lastDate := (SELECT dateColumn FROM table ORDER BY dateColumn ASC LIMIT 1);

SELECT * FROM (SELECT @lastDate as lastDate, HOUR(TIMEDIFF(dateColumn, @lastDate)) AS diff, @lastDate := dateColumn, dateColumn FROM table GROUP BY dateColumn ORDER BY dateColumn ASC) AS tbl  WHERE tbl.diff > 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM