具体步骤
步骤一: 查找所有的重复数据方法一
select count(*)from exhibition_orders eo join (select student_id, exhibition_id from exhibition_orders group by student_id, exhibition_id having count(*) > 1) eo2 on eo2.student_id = eo.student_id and eo2.exhibition_id = eo.exhibition_id
方法二
select count(*)from exhibition_orderswhere (exhibition_id, student_id) in (select exhibition_id, student_id from exhibition_orders group by student_id, exhibition_id having count(*)>1) ;步骤二:重复数据中最小编号
# 重复数据中最小编号select count(*)from exhibition_orderswhere id in (select min(id) from exhibition_orders group by student_id, exhibition_id having count(*)>1) ;
在X中,排除Y部分数据,就是需要删除的数据
select count(*) from exhibition_orderswhere id in(select idfrom exhibition_orderswhere (exhibition_id, student_id) in (select exhibition_id, student_id from exhibition_orders group by student_id, exhibition_id having count(*)>1))and id not in(select idfrom exhibition_orderswhere id in (select min(id) from exhibition_orders group by student_id, exhibition_id having count(*)>1))步骤四:删除
删除表是当前表,记得给表取别名
# 需要删除的数据 delete from exhibition_orderswhere id in(select a.id from (select idfrom exhibition_orderswhere (exhibition_id, student_id) in (select exhibition_id, student_id from exhibition_orders group by student_id, exhibition_id having count(*)>1)) as a)and id not in(select b.id from (select idfrom exhibition_orderswhere id in (select min(id) from exhibition_orders group by student_id, exhibition_id having count(*)>1)) as b) ;
创建唯一索引,源头控制
create unique index exhibition_orders_exhibition_id_student_id_uindex on exhibition_orders (exhibition_id, student_id);
本站所有软件信息均由用户上传发布,版权归原著所有。如有侵权/违规内容,敬请来信告知邮箱:764327034@qq.com,我们将及时撤销! 转载请注明出处:https://www.ssyg068.com/kuaixun/2036.html
发表回复
评论列表(0条)