您的位置:首页 > 数据库 > MySQL

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

2016-07-12 17:15 471 查看
以为通过命令直接删除就可以了,总是报错:



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)

发现总是删错数据,还未得到验证这种查询是否可行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库 mysql