mysql 删除重复项

在table中,由于之前没有设置primary key,导致代码运行replace 等sql语句时重复插入了数据库项。在整理数据库时,需要删除重复项,并保留一项。使用了如下sql 语句。


下面这个语句会删除guide重复项,并保留time最大的那项。

创建临时表
create table tmp (select * from (select * from sys_guide where uid = 4) a group by a.guide having count(a.guide)>1);
删除数据
delete from sys_guide where uid in (select uid from tmp) and guide in (select guide from tmp);
重新插入临时表数据
insert into sys_guide (select * from tmp);
删除临时表
drop table tmp;


用下面这个sql语句来检查重复项是否删除,如果结果为空,则重复项已被删除。

select  *  from sys_guide  where uid = 4 group  by  guide   having  count(guide) > 1


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值