Mysql 查询重复的记录

 

 

我们都会使用distinct去除重复值,今天调试一个问题,业务需要查询出重复的数据,实现如下:

 

查询用户和标签的关系,其中user_id,tag_id可以唯一确定一条记录:

先使用user_id, tag_id分组,之后统计count值,最后通过having过滤出count大于1的记录

select   user_id, tag_id, count(*) cnt
from g_tag 
group by user_id, tag_id 
having cnt > 1
order by tag_id desc

  

 当然了,如果具体场景中是通过tag_id就可以唯一确定一条记录的话,那就在group by分组时候只需指定tag_id即可

 

参考:http://www.pchou.info/open-source/2015/04/14/sql-duplicate-delete.html

原文地址:https://www.cnblogs.com/quan-coder/p/8268595.html