我有下表,如果超过1行包含相同的'user_badge_name'和'user_email',则认为是重复的。
user_id | user_name | user_badge_name | user_email
--------------------------------------------------
234 | Kylie | ky001 | kylie@test.com
235 | Francois | FR007 | france@test.com
236 | Maria | MA300 | Marie@test.com
237 | Francine | FR007 | france@test.com
我需要显示复制'user_badge_name'和'user_email'的那些行的user_ids和username。
我尝试了以下sql,但它没有返回所有user_ids,只返回第一个id
SELECT user_id, username , COUNT(user_badge_name) AS user_badge_name_Count FROM user GROUP BY user_badge_name HAVING user_badge_name_Count > 1
任何建议都表示赞赏
答案 0 :(得分:3)
select a.user_id, a.user_name
from user as a
inner join
(SELECT user_badge_name, user_email
FROM user
GROUP BY user_badge_name, user_email
HAVING count(*)>1
) as dups
on a.user_badge_name=dups.user_badge_name and a.user_email=dups.user_email
order by a.user_badge_name, a.user_email
答案 1 :(得分:1)
如果要查看同一行中的所有用户ID,则可以使用GROUP_CONCAT
:
SELECT GROUP_CONCAT(user_id) AS user_ids, GROUP_CONCAT(username) AS usernames, COUNT(user_badge_name) AS user_badge_name_Count FROM user GROUP BY user_badge_name HAVING user_badge_name_Count > 1
那会给你这样的东西:
user_ids | usernames | user_badge_name_Count
-----------------------------------------------
235,237 | Francois,Francine | 2