找到重复项后的MySql更新记录

时间:2017-03-22 16:53:15

标签: mysql sql

我已经成功创建了一个SQL查询,在我的表中找到重复的内容,如下所示:

SELECT email, COUNT(*) c FROM subscribers GROUP BY email HAVING c > 1 ;

此表还包含“已取消订阅”和“退回”以及“投诉”的列。

对于这些列的默认值为“0”,当用户选择退出时为“1”。

如果我的SELECT中找到的一个副本在任何这些列中都有一个“1”,我需要更新该记录中该列的“1”的其他重复记录。

1 个答案:

答案 0 :(得分:1)

你可以通过自我加入来做这样的事情:

select *
from subscribers s
  inner join subscribers i
    on s.email = i.email
      and s.id <> i.id
where 1 in (s.unsubscribed,s.bounced,s.complaint)
  and (
       i.unsubscribed<>s.unsubscribed
    or i.bounced<>s.bounced
    or i.complaint<>s.complaint
    )

对于更新,我可能会这样做,以涵盖所有可能取消订阅1条记录并且同一封电子邮件的另一条记录可能是投诉等的情况:

update subscribers s
  inner join (
    select 
        email
      , max(unsubscribed) as unsubscribed
      , max(bounced)      as bounced
      , max(complaint)    as complaint
    from subscribers as i
    group by email
    having count(*)>1 
    ) as a
      on a.email = s.email
set s.unsubscribed = a.unsubscribed
  , s.bounced      = a.bounced
  , s.complaint    = a.complaint;

rextester演示:http://rextester.com/RGOG61470

您可以将having更改为:

having count(*)>1 
  and (min(unsubscribed)<>max(unsubscribed)
    or min(bounced)     <>max(bounced)
    or min(complaint)   <>max(complaint)
       )

进一步将更新限制为仅对那三列中至少有一列具有不同值的更新。