刪除重複記錄mysql
#一、查詢重複記錄sql
SELECT id,COUNT(*) AS total,songurl FROM HFMedia.HFSongs GROUP BY songurl HAVING total > 1 ORDER BY total DESC測試
#二、創建臨時表
CREATE TEMPORARY TABLE tbl_hfsongs( id INT(10) ,number INT(5), url VARCHAR(255) , KEY pk(id) ) ENGINE=MEMORY;url
#三、查數據插入臨時表table
INSERT INTO tbl_hfsongs SELECT id,COUNT(*) AS total,songurl FROM HFMedia.HFSongs GROUP BY songurl HAVING total > 1 ORDER BY total DESC ;數據
#四、測試生產的sqls腳本
SELECT CONCAT('delete from FROM HFMedia.HFSongs where songurl = \'',uniq,'\' and id != ',id) FROM tbl_hfsongs LIMIT 2;
#五、生成執行腳本、
SELECT CONCAT('delete from FROM HFMedia.HFSongs where songurl = \'',uniq,'\' and id != ',id,';') INTO OUTFILE '/tmp/delect_sb.sql' FROM tbl_hfsongs ;查詢
#六、執行腳本tab
mysql>source /tmp/delect_sb.sql di
#七、測試清理重複數據後狀況
SELECT id,COUNT(*) AS total,songurl FROM HFMedia.HFSongs GROUP BY songurl HAVING total > 1 ORDER BY total DESC
#8,手動釋放臨時表
mysql>truncate tbl_hfsongs;
mysql>drop table tbl_hfsongs;