sql - How would you select records from a table based on the difference between 'created' dates with MySQL? -
here dummy data:
| id | created @ | | 367 | 2014-05-28 22:55:36 | | 367 | 2014-05-28 22:57:06 | | 369 | 2014-05-28 23:06:02 | | 369 | 2014-05-28 23:08:05 | | 369 | 2014-05-28 23:18:07 | | 350 | 2014-05-28 23:12:56 | | 261 | 2014-05-28 21:17:11 | | 261 | 2014-05-29 22:27:43 |
what i'd select this, ids (obviously not primary key in case) created_at
date has difference of 24hrs or more. in case above data, id 261
has 2 records in there, created on 24hrs apart. in collection returned i'd want see id 261
in there.
what effective way structure kind of query?
slower option
select id, time_to_sec(timediff(max(created_at),min(created_at))) seconds_difference table group id having seconds_difference > 3600*24
faster option
select t1.id, time_to_sec(timediff(t2.created_at, t1.created_at) seconds_difference table t1 inner join table t2 on (t2.id = t1.id , t2.created_at > t1.created_at) time_to_sec(timediff(t2.created_at, t1.created_at) > 3600*24
Comments
Post a Comment