数据库
首页 > 数据库> > SQL 去除数据库中的重复记录

SQL 去除数据库中的重复记录

作者:互联网

背景:存数据的时候多存了,导致数据重复

思路:group by 对重复的字段进行分组,重复意味着group by count的结果大于1,将count大于1的字段的最小id保留,其余删除

语句

delete from base_cell_extend where   data_category =6 and data_key='device_num' and 
cgi in (select cgi from base_cell_extend where  data_category =6 and data_key='device_num' group by cgi having count(cgi) > 1 )
and base_cell_extend.id not in (select min(base_cell_extend.id) from base_cell_extend where  data_category =6 and data_key='device_num' group by cgi having count(cgi) > 1 );

注意:最好还是能够在删除之前先select,并且计算一下删除后的数据总数是否正确,再进行delete

标签:cgi,group,extend,数据库,cell,base,SQL,重复记录,data
来源: https://blog.csdn.net/CHERISHGF/article/details/122923075