删除一个表中重复的数据

需求:删除一个表中,订单id和产品id相同的重复数据。具体需求具体分析,原理都是一样的。
1.查看是否含有重复的数据
select order_id,product_id,count(*)
from product_commit
group by order_id,product_id having count(*)>1
 
2.备份表,以免删除错误
create table bak_product_comment_181119
as
select * from product_comment;
或者
create table bak_product_comment_181119
like product_comment;//创建表结构
insert into bak_product_comment_181119
select * from product_comment;
 
3.执行删除sql语句
delete a
from product_comment a
join(
select order_id,product_id,min(comment_id) as comment_id
from product_comment
group by order_id,product_id
having count(*)>=2
) b on a.order_id=b.order_id and a.product_id=b.product_id
and a.comment_id > b.comment_id

 

 

posted on 2018-12-03 22:44  走-走  阅读(156)  评论(0编辑  收藏  举报