刪除一個表中重複的數據

需求:刪除一個表中,訂單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
相關文章
相關標籤/搜索