MySQL中删除表中重复数据,只保留一条

以为通过命令直接删除就可以了,总是报错:


delete from test


where name in(select name from test as t having count(name)>1)

发现在这只能建立临时表格,方法1设计可行:


1. 首先先创建一个临时表,然后将author表中无重复的数据拎出来,放进临时表中。
create temporary table 表名
select distinct id,name,password 
from author

然后将author表中的记录全部删除。
delete from author

最后将临时表中的记录插入author表中
insert into author (id,name,password)
select id,name,password 
from 临时表


2.方法2直接使用语句:

delete from test
where name in(select t.name from (select name from test) as t having count(t.name)>1)
and name not in(select min(id) from(select id,name from test group by name)as tt having count(tt.name)>1)

发现总是删错数据,还未得到验证这种查询是否可行

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值