1. 程式人生 > >刪除一個表中重複的資料

刪除一個表中重複的資料

需求:刪除一個表中,訂單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