如何删除从先前插入的行在一分钟内插入的行

时间:2018-06-25 10:46:17

标签: mysql sql

我有一张产品视图报告表。每次在产品页面上的点击都会添加到该表中,其中包含用户IP和DateTime的数据。

表结构:

CREATE TABLE `oc_product_viewed_report` (
  `viewed_id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `store_id` smallint(5) unsigned NOT NULL,
  `user_ip` varchar(50) DEFAULT NULL,
  `datetime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`viewed_id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8

样本数据:

+-----------+------------+----------+---------+---------------------+
| viewed_id | product_id | store_id | user_ip | datetime            |
+-----------+------------+----------+---------+---------------------+
|        10 |      10051 |        5 | ::1     | 2018-06-24 15:15:57 |
|        11 |       9594 |       16 | ::1     | 2018-06-24 15:16:29 |
|        12 |       9596 |        5 | ::1     | 2018-06-24 15:16:33 |
|        13 |       9594 |       16 | ::1     | 2018-06-24 15:16:35 |
|        14 |       9594 |       16 | ::1     | 2018-06-24 15:16:37 |
|        15 |       9594 |       16 | ::1     | 2018-06-24 15:16:58 |
|        16 |       9596 |        5 | ::1     | 2018-06-24 15:16:59 |
|        17 |       9594 |       16 | ::1     | 2018-06-24 15:17:05 |
|        18 |       9918 |       16 | ::1     | 2018-06-24 15:17:07 |
|        19 |      10047 |        5 | ::1     | 2018-06-24 16:47:36 |
|        20 |      10047 |        5 | ::1     | 2018-06-24 16:48:04 |
+-----------+------------+----------+---------+---------------------+

现在,我要删除垃圾邮件点击数据。因此,如何删除一行,该行在每个产品先前插入的行日期时间之后的1分钟内立即插入。

有可能吗?如果是的话,请帮帮我,

谢谢

1 个答案:

答案 0 :(得分:0)

您可以尝试一下

Delete from oc_product_viewed_report R1
where exists 
(select id from  oc_product_viewed_report R2 where R2.datetime between R1.datetime and  DATE_ADD (R1.datetime ,INTERVAL +1 MINUTE  and R1.id > R2.id )