我有一张表:
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'
我需要过滤结果集,以便每条记录的“距离”为20分钟:
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'
[错误的问题:哪种方式最好(也是最快)?]
正确的问题:有人可以给我一些建议如何开始吗?
修改
我在表格中添加了一个record_id字段,我尝试了这个查询:
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
但它会返回所有记录。我是在正确的方式吗?
感谢。
答案 0 :(得分:0)
加入桌子(我已经使用了名称' 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