Find duplicate concat values

点点圈 提交于 2019-12-11 07:23:54

问题


I'm trying to find each row with duplicates in a MySQL database.

1   alex    smith
2   bob     smith
3   alex    smith

I want to return:

1   alex    smith
3   alex    smith

This code will find duplicates, but it doesn't list each row that is a duplicate.

SELECT
    *,
    CONCAT(`firstName`, ' ', `lastName`) as full_name,
    COUNT(*) d
FROM users
GROUP BY full_name
HAVING d > 1;  

The code below is what I'm trying to get each row that is a dupliate, but I get the error "#1054 - Unknown column 'full_name' in 'IN/ALL/ANY subquery'"

SELECT CONCAT(`firstName`, ' ', `lastName`) as full_name
FROM users
WHERE full_name IN (
    SELECT CONCAT(`firstName`, ' ', `lastName`) as full_name
    FROM users
    GROUP BY full_name
    HAVING COUNT(full_name) > 1
)

回答1:


You cannot use aliases in WHERE clauses; and you do not actually need the CONCAT (it increases your computational costs in this case); instead you can compare "tuples".

SELECT *
FROM users
WHERE (`firstName`, `lastName`) IN (
    SELECT `firstName`, `lastName`
    FROM users
    GROUP BY `firstName`, `lastName`
    HAVING COUNT(*) > 1
);


来源:https://stackoverflow.com/questions/47819634/find-duplicate-concat-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!