根据两个字段查找重复记录

时间:2014-10-27 22:58:11

标签: mysql sql duplicates

我正在尝试修改此问题中接受的答案:Find duplicate records in MySQL,以便根据两个字段Firstname和Lastname在表格中查找重复记录。

这是原始查询,可以正常工作,并根据姓氏...

查找重复记录
SELECT Firstname, candidate.Lastname FROM candidate
INNER JOIN (SELECT Lastname FROM candidate
GROUP BY Lastname HAVING count(UserID) > 1) dup ON 
candidate.Lastname = dup.Lastname ORDER BY Lastname

现在我想在那里添加Firstname ...但我不确定我在做什么,以下不起作用:

SELECT candidate.Firstname, candidate.Lastname FROM candidate
INNER JOIN (SELECT Firstname AND Lastname FROM candidate
GROUP BY Lastname HAVING count(UserID) > 1) dup ON 
candidate.Lastname = dup.Lastname AND candidate.Firstname = dup.Firstname 
ORDER BY Lastname

1 个答案:

答案 0 :(得分:2)

看起来你的子查询中的语法有点不对,也许这就是你想要的?

SELECT candidate.Firstname, candidate.Lastname 
FROM candidate
INNER JOIN (
    SELECT Firstname, Lastname 
    FROM candidate
    GROUP BY Firstname, Lastname 
    HAVING COUNT(UserID) > 1
    ) dup 
    ON candidate.Lastname = dup.Lastname AND candidate.Firstname = dup.Firstname
ORDER BY Lastname