当前位置:网站首页>MySQL removes duplicates according to two fields

MySQL removes duplicates according to two fields

2022-07-06 21:42:00 Maple Bay River Bridge

Link to the original text : https://blog.csdn.net/Ax0592/article/details/122623533 

https://blog.csdn.net/jj89929665/article/details/124178040

delete from tbl where id not in 
(
    select a.id from 
    (
       

select id from table group by  column1, column2 having

count(column1) > 1  and count(column2) >1


    ) a
)

 

1.( Wrong operation ) Check all duplicate data
Obviously, the following code runs very slowly

select * from surface t where (select count(*) from surface where Field 1=t. Field 1 AND Field 2=t. Field 2)>1
1
So we use the following grouping

1.( Speed optimization ) Check all duplicate data
SELECT *
FROM surface
WHERE ( Field 1, Field 2, Field 3) IN (SELECT Field 1, Field 2, Field 3 FROM surface
GROUP BY Field 1, Field 2, Field 3 HAVING COUNT(*) > 1)
ORDER BY Sort field
1
2
3
4
5
2. Find duplicate data
SELECT id, Field 1, Field 2, Field 3
FROM surface
WHERE id
IN (SELECT MIN(id) FROM surface GROUP BY Field 1, Field 2, Field 3 HAVING COUNT(*) > 1)
1
2
3
4
3. Filter ( Field 1, Field 2, Field 3) All duplicate the same data , Only one bar is shown (id Minimum or maximum, etc ) data , Contains data that is not duplicated ( It is recommended to use )
SELECT *
FROM surface
WHERE id
IN (SELECT MIN(id) FROM surface GROUP BY Field 1, Field 2, Field 3)
1
2
3
4
4. Get 2 The results can be deleted through a single or multiple entries

原网站

版权声明
本文为[Maple Bay River Bridge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061315110230.html