简体   繁体   中英

Filter MySQL query on difference between timestamp fields

I have a table like:

 RCD_ID      ID           TIMESTAMP
--------|-----------|---------------------
    1,   2737738826, '2015-10-30 16:19:17'
    3,   2737738826, '2015-10-30 16:22:46'
    4,   2737738826, '2015-10-30 16:24:36'
    5,   2737738826, '2015-10-30 16:29:49'
    7,   2737738826, '2015-10-30 16:40:02'
    9,   2737738826, '2015-10-30 17:11:55'

And I need to filter the result set so each records has a 'distance' of 20 minutes each other:

 RCD_ID      ID          TIMESTAMP
--------|-----------|---------------------
    1,   2737738826, '2015-10-30 16:19:17'
    7,   2737738826, '2015-10-30 16:40:02'
    9,   2737738826, '2015-10-30 17:11:55'

[ WRONG QUESTION: Which is the best (and fastest) way to do it? ]

RIGHT QUESTION: Could someone give me some suggestion how to start from?

EDIT

I added a record_id field to the table, I try this query:

select m.*,
       (select m2.rcd_id 
        from tbl_data m2 
        where (m2.rcd_id > m.rcd_id) AND (TIMESTAMPDIFF(MINUTE, m.log_timestamp, m2.log_timestamp) >= 30)
        order by m2.rcd_id limit 1) as m2
from tbl_data as m

But it returns all the records. Am I on the right way?

Thanks.

可以将表(我使用过“ logs”)连接到表本身,并且只保留那些满足您时间限制的记录,例如:

SELECT * FROM logs AS t1, logs AS t2 WHERE t1.ID = t2.ID AND t1.TIMESTAMP != t2.TIMESTAMP AND TIMESTAMPDIFF(MINUTE, t1.TIMESTAMP, t2.TIMESTAMP) = 20

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