I’m trying to update a clients table in mySQL containing duplicate email addresses and a unique custom_ID. I would like to change duplicate email addresses by adding a ‘+’ and customer_ID before the @ sign, but only for the email addresses that are not unique.
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 | +-----------+----------------------------+
Advertisement
Answer
You can use update
with window functions and a 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;