首頁  >  文章  >  資料庫  >  Mysql刪除重複資料保留最小的id

Mysql刪除重複資料保留最小的id

一个新手
一个新手原創
2017-10-13 10:11:182101瀏覽

在網路上尋找刪除重複資料保留id最小的數據,方法如下:


DELETE
FROM
    people
WHERE
    peopleName IN (
        SELECT
            peopleName
        FROM
            people
        GROUP BY
            peopleName
        HAVING
            count(peopleName) > 1
    )
AND peopleId NOT IN (
    SELECT
        min(peopleId)
    FROM
        people
    GROUP BY
        peopleName
    HAVING
        count(peopleName) > 1
)

自己使用的時候顯示報錯:


1 delete from tb where id in (SELECT max(id) from tb GROUP BY user HAVING count(user)>1)

[Err] 1093 - You can't specify target table 'XXX' for update in FROM clause

暫時不知道是什麼原因導致的。

然後想辦法分散操作,先篩選出有重複user的數據,然後用max()選出其中較大的那一行:

SELECT max(id) from tb GROUP BY user HAVING count(user)>1

然後再根據得到的max(id)逐條刪除多餘的資料


1 delete from tb where id=xx

以上是Mysql刪除重複資料保留最小的id的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn