我有一个包含多个电子邮件ID的表,我想从表中删除重复的电子邮件ID:
TABLE:
-----------------------
|ID | Email |
| 1 | test@test.com |
| 2 | test@test.com |
| 3 | test@test.com |
| 4 | test1@test.com |
| 5 | test1@test.com |
| 6 | test1@test.com |
-----------------------
如何删除额外的电子邮件ID,表格只有唯一的电子邮件ID?
答案 0 :(得分:1)
请尝试以下查询。此查询将对您有用。
DELETE n1 FROM email n1, email n2 WHERE n1.ID > n2.ID AND n1.email = n2.email
答案 1 :(得分:0)
您可以使用以下查询删除除一个以外的所有重复记录。
DELETE FROM emails WHERE email_id IN (
SELECT email_id FROM emails GROUP BY email_id HAVING COUNT(email_id) > 1
)
其中电子邮件是表的名称,email_id是列名。
您也可以使用替代方法。将重复行表中的不同行插入新临时表。从具有重复行的表中删除数据,然后从临时表中插入不同的行
SELECT DISTINCT * INTO #tmp FROM emails
DELETE FROM emails
INSERT INTO emails
SELECT * FROM #tmp DROP TABLE #tmp
答案 2 :(得分:0)
delete table a
where id > (select min(id)
from table b
where a.email = b.email)
首先尝试将其作为选择运行,以查看它是否删除了正确的行
select *
from table a
where id > (select min(id)
from table b
where a.email = b.email)
答案 3 :(得分:0)
如果表格没有任何参考文献且尺寸不是太大,您可以尝试:
从old_tbl
创建表new_tbl SELECT *INSERT INTO new_tbl SELECT ID,来自old_tbl group的电子邮件
DROP TABLE old_tbl
RENAME TABLE new_tbl TO old_tbl
希望这有帮助
答案 4 :(得分:0)
Delete from email_table where ID NOT IN (select * from (select ID from email_table group by email) temp_table)
答案 5 :(得分:0)
试试这个
ALTER IGNORE TABLE `table_name` ADD UNIQUE INDEX index_name (`Email`);