Home > Database > Mysql Tutorial > MYSQL删除重复数据的简单方法_MySQL

MYSQL删除重复数据的简单方法_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 13:25:54
Original
1025 people have browsed it

bitsCN.com


CREATETABLE`users`(
`id`int(10)NOTNULLAUTO_INCREMENT,
`name`char(50)NOTNULL,
PRIMARYKEY(`id`)
)


deletefromuserswhereidin(selectmin(id)fromusersgroupbynamehavingcount(name)>1);

结果报错:1093youcan'tspecifytargettable....

原因是mysql删除动作不能带有本表的查询动作,意思是你删除users表的东西不能以users表的信息为条件所以这个语句会报错,执行不了。只要通过创建临时表作为查询条件。如下


deletefromuserswhereidin(select*from(selectmin(id)fromusersgroupbynamehavingcount(name)>1));

还要注意deletefromusers这里不能用别名

其他方法。

deleteusersasafromusersasa,(selectmin(id)id,namefromusersgroupbynamehavingcount(name)>1
)asbwherea.name=b.nameanda.idb.id;


建立临时表:

createtabletmp_usersselectmin(`id`),`name`fromusersgroupbyname;

truncatetableusers;
insertintousersselect*fromtmp_users;
droptabletmp_users;

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template