删除mysql中的重复行,其中时间戳间隔小于5秒

时间:2016-03-03 04:59:06

标签: mysql

我有一台服务器,每次访问都会向数据库添加几条记录。服务器在每次访问的2-5条记录之间做到了。之间的所有数据都是相同的期望时间戳。我想每次访问只保留1条记录。并删除其他记录。

之前:

+----+----------------------+------+--------+
| id |      timestamp       | user | action |
+----+----------------------+------+--------+
|  1 | 01-01-01-16 03:02:05 |    5 | visit  |
|  2 | 01-01-01-16 03:02:06 |    5 | visit  |
|  3 | 01-01-01-16 03:02:05 |    6 | watch  |
|  4 | 01-01-01-16 03:02:06 |    6 | watch  |
|  5 | 01-01-01-16 03:04:05 |    9 | visit  |
|  6 | 01-01-01-16 03:04:06 |    9 | visit  |
|  7 | 01-01-01-16 03:04:07 |    9 | visit  |
|  8 | 01-01-01-16 03:04:35 |    2 | see    |
+----+----------------------+------+--------+

预期结果:

+----+----------------------+------+--------+
| id |      timestamp       | user | action |
+----+----------------------+------+--------+
| 1, | 01-01-01-16 03:02:05 |    5 | visit  |
| 3, | 01-01-01-16 03:02:05 |    6 | watch  |
| 4, | 01-01-01-16 03:02:06 |    6 | watch  |
| 5, | 01-01-01-16 03:04:05 |    9 | visit  |
| 8, | 01-01-01-16 03:04:35 |    2 | see    |
+----+----------------------+------+--------+

换句话说:如您所见,第1,2行相同。相同的用户,只有一秒钟。所以我删除第2行。第4行我没有碰,因为它不是访问

我开始查询:delete from table where id in(...)

2 个答案:

答案 0 :(得分:1)

DELETE FROM visit 
       WHERE id IN (
               SELECT v2.id 
               FROM visit v1           
               JOIN visit v2
                 ON v1.id < v2.id
                AND v1.user = v2.user                    
                AND TIMESTAMPDIFF(SECOND, v1.timestamp, v2.timestamp) < 5  
               WHERE v1.action = 'visit'         
                 AND v2.action = 'visit'                              
               ) 

答案 1 :(得分:1)

使用join更快地执行查询

DELETE t1 FROM visit t1
INNER JOIN visit t2 ON v1.user = v2.user
WHERE v1.id < v2.id AND
TIMESTAMPDIFF(SECOND, t1.timestamp, t2.timestamp) < 5 AND 
t1.action = 'visit' AND
t2.action = 'visit'