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

mysql删除表中某一字段重复的记录

答案:

比如,表:event(id int(10) auto_increment primary key,
sid int(10)not null,
detail text)

我想删除表event中sid重复的记录,请问有没有这样SQL语句?或是通过其它方法?

复制代码 代码如下:

delete from event as e
where id != (select min(id) from event where sid=e.sid);

or
复制代码 代码如下:

delete from event
where sid not in (select mid from (select sid ,min(id) as mid from event group by sid))

应该是有用的代码
复制代码 代码如下:

alter ignore table event add unique index idu_sid (sid);
alter table event drop index idu_sid;

上一个:MySQL忘记密码恢复密码的实现方法
下一个:log引起的mysql不能启动的解决方法

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