Delete Duplicate email addresses from Table in MYSQL(从 MYSQL 中的表中删除重复的电子邮件地址)
问题描述
我有一个表,其中包含 ID
、firstname
、lastname
、address
、email 的列
等等.
I have a table with columns for ID
, firstname
, lastname
, address
, email
and so on.
有什么方法可以从 TABLE 中删除重复的 email
地址?
Is there any way to delete duplicate email
addresses from the TABLE?
其他信息(来自评论):
Additional information (from comments):
如果有两行具有相同的 email
地址,其中一行将具有正常的 firstname
和 lastname
,而另一行将具有 firstname
中的即时".因此,我可以区分它们.我只想删除名字为instant"的那个.
If there are two rows with the same email
address one would have a normal firstname
and lastname
but the other would have 'Instant' in the firstname
. Therefore I can distinguish between them. I just want to delete the one with first name 'instant'.
注意,firstname='Instant'
的某些记录只有 1 个 email
地址.我不想只删除一个唯一的电子邮件地址,所以我不能只删除 firstname='Instant'
的所有内容.
Note, some records where the firstname='Instant'
will have just 1 email
address. I don't want to delete just one unique email address, so I can't just delete everything where firstname='Instant'
.
请帮帮我.
推荐答案
我不知道这是否可以在 MYSQL 中工作(我没有使用它)...但是您应该可以执行以下操作片段.
I don't know if this will work in MYSQL (I haven't used it)... but you should be able to do something like the following snippets.
我建议您运行它们以了解是否选择了正确的数据.如果它确实有效,那么您可能想要在列上创建一个约束.
I'd suggest you run them in order to get a feel for if the right data is being selected. If it does work, then you probably want to create a constraint on the column.
获取所有重复的电子邮件地址:
Get all of the duplicate e-mail addresses:
SELECT
EMAILADDRESS, COUNT(1)
FROM
TABLE
GROUP BY EMAILADDRESS
HAVING COUNT(1) > 1
然后确定给出的 ID:
Then determine the ID from that gives:
SELECT
ID
FROM
TABLE
WHERE
EMAILADDRESS IN (
SELECT
EMAILADDRESS
FROM
TABLE
GROUP BY EMAILADDRESS
HAVING COUNT(1) > 1
)
最后,根据上述和其他约束,删除行:
Then finally, delete the rows, based on the above and other constraints:
DELETE
FROM
TABLE
WHERE
ID IN (
SELECT
ID
FROM
TABLE
WHERE
EMAILADDRESS IN (
SELECT
EMAILADDRESS
FROM
TABLE
GROUP BY EMAILADDRESS
HAVING COUNT(1) > 1
)
)
AND FIRSTNAME = 'Instant'
这篇关于从 MYSQL 中的表中删除重复的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 MYSQL 中的表中删除重复的电子邮件地址


- 更改自动增量起始编号? 2021-01-01
- SQL 临时表问题 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01