1. 程式人生 > >sql刪除重復數據

sql刪除重復數據

錯誤 having article lec get light sel ecif 並且

用爬蟲爬了一些數據,但是有些標題是重復的,需要刪除,所以找了一下刪除重復標題數據的sql。

# 查詢所有重復的數據
select * 
FROM
	tb_xici_article 
WHERE
	post_title IN ( SELECT post_title FROM tb_xici_article GROUP BY post_title HAVING count( post_title ) > 1 )
	
	
# 查詢所有重復並且id不是最小的那些重復數據
SELECT
	* 
FROM
	tb_xici_article 
WHERE
	post_title IN ( SELECT post_title FROM tb_xici_article GROUP BY post_title HAVING count( post_title ) > 1 ) 
	AND id NOT IN ( SELECT min( id ) FROM tb_xici_article GROUP BY post_title HAVING count( post_title ) > 1 )
	

# 刪除重復數據
如果直接按下面這樣寫,mysql會報You can‘t specify target table for update in FROM clause錯誤,需要把select出的結果再通過中間表select一遍
DELETE
FROM
	tb_xici_article 
WHERE
	post_title IN ( SELECT post_title FROM tb_xici_article GROUP BY post_title HAVING count( post_title )
	> 1 ) and id not in (select min(id) from tb_xici_article group by post_title HAVING count(post_title) > 1)
	
#	最終版
DELETE 
FROM
	tb_xici_article 
WHERE
	id IN (
	SELECT
		temp.id 
	FROM
		(
		SELECT
			* 
		FROM
			tb_xici_article 
		WHERE
			post_title IN ( SELECT post_title FROM tb_xici_article GROUP BY post_title HAVING count( post_title ) > 1 ) 
			AND id NOT IN ( SELECT min( id ) FROM tb_xici_article GROUP BY post_title HAVING count( post_title ) > 1 ) 
		) temp 
	)
	

  

sql刪除重復數據