当前位置:数据库 > MySQL >>

mysql怎么删除掉标题重复的记录

就是删除table这个表title字段重复的记录,保留id大的记录,或者随便保留一个也行。 怎么写呢?
追问:提示
[Err] 1093 - You can't specify target table 'wp_posts' for update in FROM clause提示这个

[SQL] delete from wp_posts t1
where exists(select 1 from wp_posts where post_title = t1.post_title and id > t1.id) 

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't1
where exists(select 1 from wp_posts where post_title = t1.post_title and id ' at line 1
答案:吃饭中…… 
保留最大的ID:
delete  from table where id not in(select max(id) from table group by title) 

保留最小的ID:
delete  from table where id not in(select min(id) from table group by title)
其他:delete from table t1
where exists(select 1 from table where title = t1.title and id > t1.id) --第一步 
--备份整张表   到临时表中 
select   distinct   id,name,sex   --如果这里还有很多字段名,请自己加上 
  into   #linshibiao   from   表名 

--第二步 
--初始化表 
truncate   table   表名 

--第三步 
--写数据 
insert   into   表名   (id,name,sex)   --如果这里还有很多字段名,请自己加上 
select   id,name,sex   --如果这里还有很多字段名,请自己加上 
from   表名 MYSQL 数据库 表内UserName字段存在很多重复记录 求一个删除的语句,要求重复记录保留一条ID最小的。 delete from radcheck where username in (select 

上一个:无法载入com.mysql.jdbc.Driver驱动程序! 无法连接数据库!
下一个:Mysql一条数据大概会占用多少容量

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,