简体   繁体   English

MySQL查询基于时间戳的一部分删除重复项

[英]Mysql query to delete duplicates based on part of timestamp

I have a webshop which has gotten some duplicate orders because of paypal sending some payment notifications twice or even 3-4-5 times, with random interval between them, ranging from seconds to minutes. 我有一个网上商店,由于Paypal发送一些付款通知两次甚至3-4-5次,两次通知之间有随机间隔,从几秒到几分钟不等,因此得到了一些重复的订单。

Example from my orders-table: 我的订单表中的示例:

ID     productname    buyer    timestamp
1      apples         john     2014-07-01 19:22:20
2      bananas        john     2014-07-01 19:22:20
3      oranges        mary     2014-07-01 19:22:52
4      apples         john     2014-07-01 19:22:53
5      bananas        john     2014-07-01 19:22:53
6      apples         chris    2014-07-01 19:22:54

I want to delete all duplicate rows, and by duplicate i mean that productname, buyer and date of purchase should be equal. 我要删除所有重复的行,重复是指产品名称,买方和购买日期应该相等。 (only date, not the time part of the timestamp) (仅日期,而不是时间戳记的时间部分)

So from the table above, this should be deleted: 因此,在上表中,应将其删除:

4      apples         john     2014-07-01 19:22:53
5      bananas        john     2014-07-01 19:22:53

Because he bought the same things earlier the same day. 因为他在同一天早些时候买了同样的东西。

How would you go about doing this? 您将如何去做?

you can do it with EXISTS 你可以用EXISTS来做

DELETE FROM table AS t
WHERE EXISTS(
    SELECT * 
    FROM table 
    WHERE id != t.id 
        AND productname = t.productname 
        AND buyer = t.buyer 
        AND DATE(timestamp) = DATE(t.timestamp))

Here is another similar question . 这是另一个类似的问题 What it really comes down to is making the id a unique key field so that you can't insert duplicates in the first place. 真正的原因是使id成为唯一的键字段,以便您一开始就不能插入重复项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM