简体   繁体   中英

Find duplicates in SQL column

I've got this table named log:

ID   User_ID   Machine_Number   Email 
1     100         12345          jim@gmail.com
2     100         12345          jim@gmail.com
3     101         67890          bill@gmail.com
4     102         12345          steve@gmail.com

I need to find the User_IDs of users with the same Machine_Number. In this case, I need a query that returns 100 and 102.

I've tried:

SELECT user_id, COUNT(machine_number) 
FROM log
GROUP BY machine_number
HAVING COUNT(machine_number) > 1

but that gives the count of each occurrence of the machine_number, ie User_ID Count(machine_number)

100          2
101          1
102          1

Any suggestions?

I guess you want to get both user_id against same machine. Try group_concate :

SELECT group_concat(DISTINCT user_id), machine_number
FROM log
GROUP BY machine_number
HAVING COUNT(machine_number) > 1

I assume you mean different uses with the same machine. You can use exists like this to get the original rows:

select l.*
from log l
where exists (select 1
              from log l2 
              where l2.machine_number = l.machine_number and l2.email <> l.email
             );

This should have very good performance, particularly with an index on (machine_number, email) .

use exists

select distinct t1.* from log t1 where exists
( select 1 from log t2 where t1.machine_number=t2.machine_number
 group by machine_number having count(*)>1
)
SELECT user_id,Machine_Number 
FROM log where Machine_Number IN 
     (SELECT Machine_Number FROM log GROUP BY Machine_Number HAVING count(1) > 1) 
ORDER BY Machine_Number;

And if you want distinct user_id then use below.

SELECT distinct user_id, Machine_Number
FROM log where Machine_Number IN
(SELECT Machine_Number FROM log GROUP BY Machine_Number HAVING count(1) > 1) 
ORDER BY Machine_Number;

below code is in MS SQL syntax, but I think it will work fine in MYSQL.

 SELECT DISTINCT t1.User_ID FROM log t1 
    INNER JOIN 
     (
    SELECT l.Machine_Number,count(DISTINCT l.User_ID)Count FROM log l
    GROUP BY l.Machine_Number
    )t2
    ON 
    t1.Machine_Number =t2.Machine_Number
    WHERE t2.Count>1
Select DISTINCT M.User_Id, M.Machine_NUmber from Machine M  
Inner Join (Select M1.Machine_NUmber from Machine M1 Group By M1.Machine_NUmber Having COUNT(M1.Machine_NUmber)>1) M2 
On M.Machine_NUmber = M2.Machine_NUmber

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM