就是連URL都一樣的那種(名字一樣的也可以參考處理),怎么辦呢?
當(dāng)然是要?jiǎng)h掉了。
1、首先查一下看看重復(fù)了多少,按url分組,并且count大于1的就說明是重復(fù)的。
select url,count(*) from yg_articles t group by t.url having count(*)>1
2、我這里查到是有1500條重復(fù),都是重復(fù)了1次,那么我們就把id大的那個(gè)刪掉就行,先把大id查出來
select max(id) from yg_articles t group by t.url having count(*)>1
3、然后把大id刪掉
delete from yg_articles
where id in (select max(id) from yg_articles t group by t.url having count(*)>1)
不出意外的報(bào)錯(cuò)了,[Err] 1093 - You can't specify target table 'yg_articles ' for update in FROM clause
不要慌,這個(gè)意思是說,你不能用yg_articles的子查詢來刪yg_articles,典型的我殺我自己,這樣容易死鎖,所以被禁用了
怎么辦呢??jī)蓚€(gè)方法,一種是搞個(gè)臨時(shí)表b,把查到的結(jié)果先insert進(jìn)去,然后delete from yg_articles where id in (select id from b)就搞定了。
還有一種更簡(jiǎn)單的,就是把子查詢弄個(gè)別名,當(dāng)成一個(gè)子表b來用,效果也是一樣。
先select驗(yàn)證一下出來的條數(shù)對(duì)不對(duì),我有一次不小心寫錯(cuò)sql就刪除了全表(還好有備份)
select * from yg_articles
where id in (
select id from (select max(id) id from yg_articles t group by t.url having count(*)>1) b
)
然后執(zhí)行刪除,真正執(zhí)行前,一定要先備份,一定要先備份,一定要先備份,
delete from yg_articles
where id in (
select id from (select max(id) from yg_articles t group by t.url having count(*)>1) b
)
是不是so easy。
最后,如果你重復(fù)的不是2個(gè),而是3個(gè)、4個(gè)怎么辦呢?刪最大的,刪完了還是有重復(fù)的。
——那就多執(zhí)行幾次。最后他就只剩一個(gè)了。
或者你反著寫一下sql,刪除所有存在重復(fù)的id,并且not id 最小id的子查詢,這樣就可以一次搞定。
#扣扣376665005
delete from yg_articles
where url in (
select url from (select url from yg_articles t group by t.url having count(*)>1) b
)
and id not in (
select id from (select min(id) as id from yg_articles t group by t.url having count(*)>1) c
)
覺得好就轉(zhuǎn)、評(píng)、贊隨便來個(gè)吧,關(guān)注一下也是可以的~
bye bye~