2013-06-19 74 views
1

我有一個腳本,用於檢查數據庫中的重複對,並選擇除一個之外需要刪除的所有條目。如何爲mysql刪除語句設置正確的限制

我有這樣的腳本,選擇需要刪除的第100條和正常工作:

SELECT* 
    FROM vl_posts_testing 
    INNER JOIN (
     SELECT max(ID) AS lastId, `post_content`,`post_title` 
     FROM vl_posts_testing WHERE vl_posts_testing.post_type='post' 
     GROUP BY `post_content`,`post_title` 
     HAVING count(*) > 1) duplic 
    ON duplic.`post_content` = vl_posts_testing.`post_content` 
    AND duplic.`post_title` = vl_posts_testing.`post_title` 
WHERE vl_posts_testing.id < duplic.lastId 
AND vl_posts_testing.post_type='post' 
LIMIT 0,100 

然而,當我嘗試刪除該組數據的使用:

DELETE vl_posts_testing 
    FROM vl_posts_testing 
    INNER JOIN (
     SELECT max(ID) AS lastId, `post_content`,`post_title` 
     FROM vl_posts_testing WHERE vl_posts_testing.post_type='post' 
     GROUP BY `post_content`,`post_title` 
     HAVING count(*) > 1) duplic 
    ON duplic.`post_content` = vl_posts_testing.`post_content` 
    AND duplic.`post_title` = vl_posts_testing.`post_title` 
WHERE vl_posts_testing.id < duplic.lastId 
AND vl_posts_testing.post_type='post' 
LIMIT 100 

我收到下列錯誤消息:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 10' at line 8

該腳本已經使用此答案構造https://stackoverflow.com/a/6108860/1168944

實際上,腳本在沒有設置限制的情況下對少量數據工作得很好,但是由於我在一個大表(大約600k條目)上運行它,我需要在例程中分割這個腳本由於處理器,內存等服務器限制,僅處理有限數量的數據。

還考慮到此示例:MySQL LIMIT on DELETE statement但結果不同,因爲無論限制多麼小,都不執行修改。

回答

1

幾次重試後我已經找到了一種方法,使其工作:

DELETE vl_posts_testing 
    FROM vl_posts_testing 
    INNER JOIN (
     SELECT max(ID) AS lastId, `post_content`,`post_title` 
     FROM vl_posts_testing WHERE vl_posts_testing.post_type='post' 
     GROUP BY `post_content`,`post_title` 
     HAVING count(*) > 1 
     LIMIT 0,100) duplic 
    ON duplic.`post_content` = vl_posts_testing.`post_content` 
    AND duplic.`post_title` = vl_posts_testing.`post_title` 
WHERE vl_posts_testing.id < duplic.lastId 
AND vl_posts_testing.post_type='post' 

其實我所做的是設置一個內部限制的第一組數據,爲了比較它的數據庫的其餘部分使其工作。它的工作,但我不知道這是做到這一點的正確方法。

+0

不要忘記你可以通過點擊綠色的勾號來接受你自己的答案... –

相關問題