2016-03-01 121 views
0

我有一個擁有1200000條記錄的Mysql數據庫,它擁有着名藝術家的專輯。唯一的問題是,有重複,但唯一的重複字段不是主鍵,而是標題。 PHP中有一個簡單的腳本來刪除不需要的腳本嗎?過濾數據庫並刪除重複

回答

2

刪除一行,如果有exists另一行標題相同但ID較低。

delete from tablename t1 
where exists (select * from tablename t2 
       where t2.title = t1.title 
       and t2.id < t1.id) 
0

試試這個

SELECT distinct tablename .* 
    FROM tablename as tb1 
    join tablename as tb2 
WHERE tb1.title = tb2.title 
    and tb1.id < tb2.id 

它可以幫助你獲取非重複記錄

刪除重複記錄

DELETE tablename 
    FROM tablename as tb1 
    join tablename as tb2 
WHERE tb1.title = tb2.title 
    and tb1.id < tb2.id