加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 08:18:14 所属栏目:MsSql教程 来源:网络整理
导读:比如,表:event(id int(10) auto_increment primary key, sid int(10)not null, detail text) 我想删除表event中sid重复的记录,请问有没有这样SQL语句?或是通过其它方法? 复制代码 代码如下: deletefromeventase whereid!=(selectmin(id)fromeve

比如,表: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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读