我们都会使用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

 

相关文章:

  • 2022-12-23
  • 2021-12-18
  • 2022-12-23
  • 2021-09-09
  • 2021-11-24
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
  • 2021-05-20
  • 2021-10-17
  • 2021-11-28
  • 2021-10-10
相关资源
相似解决方案