MySQL中刪除重複數據只保留一條 MySQL中刪除重複數據只保留一條

MySQL中刪除重複數據只保留一條

 

用SQL語句,刪除掉重複項只保留一條html

在幾千條記錄裏,存在着些相同的記錄,如何能用SQL語句,刪除掉重複的呢 
一、查找表中多餘的重複記錄,重複記錄是根據單個字段(peopleId)來判斷post

複製代碼
SELECT
    *
FROM
    people
WHERE
    peopleId IN (
        SELECT
            peopleId
        FROM
            people
        GROUP BY
            peopleId
        HAVING
            count(peopleId) > 1
    )
複製代碼

二、刪除表中多餘的重複記錄,重複記錄是根據單個字段(peopleId)來判斷,只留有rowid最小的記錄url

複製代碼
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
)
複製代碼

三、查找表中多餘的重複記錄(多個字段)spa

複製代碼
SELECT
    *
FROM
    vitae a
WHERE
    (a.peopleId, a.seq) IN (
        SELECT
            peopleId,
            seq
        FROM
            vitae
        GROUP BY
            peopleId,
            seq
        HAVING
            count(*) > 1
    )
複製代碼

四、刪除表中多餘的重複記錄(多個字段),只留有rowid最小的記錄code

複製代碼
DELETE
FROM
    vitae a
WHERE
    (a.peopleId, a.seq) IN (
        SELECT
            peopleId,
            seq
        FROM
            vitae
        GROUP BY
            peopleId,
            seq
        HAVING
            count(*) > 1
    )
AND rowid NOT IN (
    SELECT
        min(rowid)
    FROM
        vitae
    GROUP BY
        peopleId,
        seq
    HAVING
        count(*) > 1
)
複製代碼

五、查找表中多餘的重複記錄(多個字段),不包含rowid最小的記錄htm

複製代碼
SELECT
    *
FROM
    vitae a
WHERE
    (a.peopleId, a.seq) IN (
        SELECT
            peopleId,
            seq
        FROM
            vitae
        GROUP BY
            peopleId,
            seq
        HAVING
            count(*) > 1
    )
AND rowid NOT IN (
    SELECT
        min(rowid)
    FROM
        vitae
    GROUP BY
        peopleId,
        seq
    HAVING
        count(*) > 1
)
複製代碼

6.消除一個字段的左邊的第一位:blog

UPDATE tableName
SET [ Title ]= RIGHT ([ Title ],(len([ Title ]) - 1))
WHERE
    Title LIKE '村%'

7.消除一個字段的右邊的第一位:get

UPDATE tableName
SET [ Title ]= LEFT ([ Title ],(len([ Title ]) - 1))
WHERE
    Title LIKE '%村'

8.假刪除表中多餘的重複記錄(多個字段),不包含rowid最小的記錄it

複製代碼
UPDATE vitae
SET ispass =- 1
WHERE
    peopleId IN (
        SELECT
            peopleId
        FROM
            vitae
        GROUP BY
            peopleId
相關文章
相關標籤/搜索