我用来做
SELECT email, COUNT(email) AS occurences
FROM wineries
GROUP BY email
HAVING (COUNT(email) > 1);
根据他们的电子邮件查找重复项。
但是现在我需要他们的ID来定义要删除哪一个。
第二个约束是:我只想要LAST INSERTED重复项。
因此,如果有2个条目将test@test.com作为电子邮件,并且它们的ID分别为40和12782,那么它将仅删除12782条目并保留40条条目。
关于我如何做到这一点的任何想法?我一直在混搭SQL大约一个小时,似乎无法找到确切的方法。
谢谢,祝你有愉快的一天!
答案 0 :(得分:5)
好吧,你有点回答你的问题。您似乎想要max(id)
:
SELECT email, COUNT(email) AS occurences, max(id)
FROM wineries
GROUP BY email
HAVING (COUNT(email) > 1);
您可以使用该语句删除其他人。使用join
删除有一个棘手的语法,您必须首先列出表名,然后使用连接指定from
子句:
delete wineries
from wineries join
(select email, max(id) as maxid
from wineries
group by email
having count(*) > 1
) we
on we.email = wineries.email and
wineries.id < we.maxid;
或者将其写为exists
子句:
delete from wineries
where exists (select 1
from (select email, max(id) as maxid
from wineries
group by email
) we
where we.email = wineries.email and wineries.id < we.maxid
)
答案 1 :(得分:1)
select email, max(id), COUNT(email) AS occurences
FROM wineries
GROUP BY email
HAVING (COUNT(email) > 1);
答案 2 :(得分:0)
delete from wineries
where id not in
(
select * from
(
select min(id)
from wineries
group by email
) x
)
你需要一个子查询来欺骗MySQL从它同时选择的表中删除。
答案 3 :(得分:0)
DELETE duplicates.*
FROM wineries
JOIN wineries AS duplicates USING (email)
WHERE duplicates.id < wineries.id;
答案 4 :(得分:0)
这是最简单的选项:
DELETE FROM wineries
WHERE id NOT IN
(
SELECT MIN(id) id
FROM wineries
GROUP BY email
);
这将仅保留每个电子邮件地址的第一个插入记录,所有其他记录将被删除。应该将这个答案归功于@juergen d,因为这只是他的答案的修订版。