数据库清除重复数据
背景
历史数据里大量的有重复的电话数据需要删除,无法通过代码连接数据库删除。
注: 懒得废话,直接上过程。
执行步骤
第一步
select phone,count(phone) as cnt from messages group by phone having cnt > 1;
第二步
select id,phone where messages from phone in (第一步.phone,xxx,xxx ...);
第三步
把数据放到excel中
phone | id | 函数1 | 函数2 | |
源 | x899858339x | 3087135 | =A1=A2 | =IF(C1,"delete from messages where id = "&B1&";","") |
示例 | x899858339x | 3087135 | TRUE | delete from messages where id = 3087135; |
把函数2列复制执行就成。
其实更高效的方式在于
phone | id | 函数1 | 函数2 | |
源 | x899858339x | 3087135 | =A1=A2 | =IF(C1,B1;","") |
示例 | x899858339x | 3087135 | TRUE | 3087135 |
delete from messages where id in (函数2....)
浮生潦草闲愁广,一听啤酒一口尽