【问题标题】:SQL update : deduplication of email address by adding + ID before @SQL更新:通过在@之前添加+ ID来删除电子邮件地址的重复数据
【发布时间】:2020-09-21 15:25:55
【问题描述】:

我正在尝试更新 mySQL 中的客户表,其中包含重复的电子邮件地址和唯一的 custom_ID。我想通过在 @ 符号前添加“+”和 customer_ID 来更改重复的电子邮件地址,但仅限于不唯一的电子邮件地址。

UPDATE clients SET email = REPLACE(email,'@', CONCAT('+',custom_ID,'@'))

INPUT
+-----------+-------------------------+
| custom_ID | email                   |
+-----------+-------------------------+
|  1001     | john.smith@live.com     |
|  1002     |    evyandy@email.net    |
|  1007     |    evyandy@email.net    |
|  1012     |        ann@live.com     |
|  1020     |       rick@yahoo.com    |
|  1021     |        ann@live.com     |
|  1023     |    evyandy@email.net    |
|  1024     |       emma@gmail.com    |
+-----------+-------------------------+

 OUTPUT
+-----------+----------------------------+
| custom_ID | email                      |
+-----------+----------------------------+
|  1001     |   john.smith@live.com      |
|  1002     |      evyandy@email.net     |
|  1007     | evyandy+1007@email.net     |
|  1012     |          ann@live.com      |
|  1020     |         rick@yahoo.com     |
|  1021     |     ann+1021@live.com      |
|  1023     | evyandy+1023@email.net     |
|  1024     |         emma@gmail.com     |
+-----------+----------------------------+

【问题讨论】:

  • 您应该知道,并非所有电子邮件提供商都以相同的方式使用+。不保证user+1@host.comuser+2@host.com 会发送给同一个人。

标签: mysql sql email duplicates


【解决方案1】:

您可以将update 与窗口函数和join 一起使用:

update clients c join
       (select c.*, row_number() over (partition by email order by custom_id) as seqnum
        from clients cc
       ) cc
       on c.custom_id = cc.custom_id
    set email = concat(substring_index(email, '@', 1), '+', custom_id, '@', substring_index(email, '@', -1))
    where cc.seqnum > 1;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-30
    • 2019-12-17
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 2017-02-12
    • 2011-08-26
    相关资源
    最近更新 更多