MySQL数据库去重 SQL解决

MySQL数据库去重的方法

​ 数据库最近有很多重复的数据,数据量还有点大,本想着用代码解决,后来发现用SQL就能解决,这里记录一下

看这条SQL

DELETE consum_record
FROM
 consum_record, 
 (
  SELECT
   min(id) id,
   user_id,
   monetary,
   consume_time
  FROM
   consum_record
  GROUP BY
   user_id,
   monetary,
   consume_time
  HAVING
   count(*) > 1
 ) t2
WHERE
 consum_record.user_id = t2.user_id 
 AND consum_record.monetary = t2.monetary
 AND consum_record.consume_time = t2.consume_time
 AND consum_record.id > t2.id;

首先是将重复的记录查出来另存到一个集合(临时表t2), 集合里是每种重复记录的最小ID

consum_record.user_id = t2.user_id 
 AND consum_record.monetary = t2.monetary
 AND consum_record.consume_time = t2.consume_time
 AND consum_record.id > t2.id

这些关联是判断重复基准的字段

根据条件,删除原表中id大于t2中id的记录

到这里欢呼一句 SQL大法好

还是要多学习啊!SQL也不能拖后腿

但是在处理完成之后,数据库中的主键id不是连续的了,强迫症犯了

下面是解决办法:

​ 我这边处理方式是:先删除主键id字段,然后再重新生成主键id字段

SQL语句


posted @ 2019-11-05 11:20  MengW9  阅读(1289)  评论(0编辑  收藏  举报