1. 程式人生 > 資料庫 >mysql刪除重複行的實現方法

mysql刪除重複行的實現方法

表relation

 create table relation(
 id int primary key auto_increment,userId int not null,fanId int not null
 );

插入幾條資料

insert into relation(userId,fanId) 
values(1,1),(1,(2,2),(3,3),3);

表中的資料

id userId fanId
1 1 1
2 1 1
3 1 1
4 2 2
5 2 2
6 3 3
7 3 3

去重

delete t from relation s
join relation t using(userId,fanId)
where s.id<t.id;

總結

以上所述是小編給大家介紹的mysql刪除重複行的實現方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!