我有mysql
表格contacts
,如下所示
id | email | mobile |
1 | abc123@xyz.com | 9800000023 |
2 | abc123@xyz.com | 9800000021 |
3 | pqr111@xyz.com | 9800000034 |
4 | pqr123@xyz.com | 9900000033 |
5 | abc111@xyz.com | 9804400000 |
6 | pqr111@xyz.com | 9800500000 |
7 | abc111@xyz.com | 8600000000 |
我想将重复的电子邮件值(第一次出现除外)更新为null
或空白而不影响mobile
列。
解决方案应该看起来像
id | email | mobile |
1 | abc123@xyz.com | 9800000023 |
2 | <null> | 9800000021 |
3 | pqr111@xyz.com | 9800000034 |
4 | pqr123@xyz.com | 9900000033 |
5 | abc111@xyz.com | 9804400000 |
6 | <null> | 9800500000 |
7 | <null> | 8600000000 |
是否有办法IGNORE
重复电子邮件列值,但应将mobile
值插入表中。
答案 0 :(得分:1)
update contacts
set email = null
where id not in
(
select * from
(
select min(id)
from contacts
group by email
) tmp
)