我有以下Mysql选择,它返回表格中所有重复的电子邮件地址。
SELECT users.FirstName, `Surname`, users.email1
FROM users
INNER JOIN (
SELECT email1
FROM users
GROUP BY email1
HAVING count(email1) > 1) dup ON users.email1 = dup.email1
ORDER BY users.email1
这很好用,我现在要做的是用“YES”更新名为users.DupEmail的字段,但我不确定如何做到这一点。我已经尝试使用更新行来替换选择,但我显然错了,因为我回到这里寻求帮助。
有人可以帮忙吗?
答案 0 :(得分:2)
您可以将多表UPDATE
语法与自联接使用:
UPDATE users AS u1
JOIN users AS u2 USING (email1)
SET u1.DupEmail = 'YES'
WHERE NOT u1.id = u2.id; -- use your primary key here
答案 1 :(得分:0)
以下脚本必须解决您的问题 - 安全并使用SELECT在应用更新之前测试结果:
-- SELECT FirstName, Surname, email1
-- FROM users
UPDATE users
SET DupEmail = 1
WHERE u0.email1 IN (
SELECT u1.email1
FROM users u1
GROUP BY u1.email1
HAVING COUNT(*) > 1
)