Specific duplicate selection in MySQL
-
20-04-2021 - |
题
I did take a look in and outside of SO and still don't know if this can be done. I have a table that looks like this:
User ID | Role | First Name | Last Name | Email |<br>
0001 | K | John | Smith | e@e.co|<br>
0002 | Q | Jane | Dickens | q@q.co|<br>
0003 | K | John | Smith | e@e.co|<br>
0004 | J | Jack | Paper | j@j.co|<br>
As you can see, the table contains a duplicate due to a user entering their information two separate times. I want to display the rows that
I can get the first three conditions to work with an inner join subquery, but I get 0 returned results when ever I try to to add in the fourth condition.
Thanks in advance for your help!
解决方案
SELECT GROUP_CONCAT(`User ID`) as IDs, Role, `First Name`, `Last Name`, Email
FROM users_table
GROUP BY Role,`First Name`,`Last Name`,Email
Will give a table like
IDs | Role | First Name | Last Name | Email |
0001,0003 | K | John | Smith | e@e.co|
0002 | Q | Jane | Dickens | q@q.co|
0004 | J | Jack | Paper | j@j.co|
The trick is to GROUP BY
everything except ID
.
You could also do:
SELECT COUNT(`User ID`) as numIDs, GROUP_CONCAT(`User ID`) as IDs,
Role, `First Name`, `Last Name`, Email
FROM users_table
GROUP BY Role,`First Name`,`Last Name`,Email
HAVING numIDs > 1
to get
numIDs |IDs | Role | First Name | Last Name | Email |
2 |0001,0003 | K | John | Smith | e@e.co|
Anyhow, you get the point of how to vary it to your purposes.
其他提示
Try something like:
select *
from tb_users
where (first_name, last_name, email) in
(select first_name, last_name, email
from tb_users
group by first_name, last_name, email
having count(*) > 1)
I am assuming that your table does not contain true duplicate rows (where the User ID also matches). I think it should be as simple as this to get the relevant rows back (with my adjusted column and table naming scheme):
SELECT ui.user_id, ui.role, ui.first_name, ui.last_name, ui.email
FROM user_info AS ui
INNER JOIN user_info AS udi
ON ui.first_name = udi.first_name
AND ui.last_name = udi.last_name
AND ui.email = udi.email
AND ui.user_id != udi.user_id;
I would use count(*) and group by clause.
Like this
SELECT count(*) as count
FROM table group by concat(firstname,'\n',last_name) having count
=1;
I think you want to delete duplicate rows. Only take count by group by and delete duplicate rows.
select * from tb_users
group by first_name, last_name, email
having count(*) > 1