【笔记】MySQL删除重复记录保留一条

DELETE
FROM
    ex_digitalmoney_account
WHERE
    coinCode = 'NST'
AND id IN (
    SELECT
        id
    FROM
        (
            SELECT
                id
            FROM
                ex_digitalmoney_account
            WHERE
                coinCode = 'NST'
            AND customerId IN (
                SELECT
                    customerId
                FROM
                    ex_digitalmoney_account
                WHERE
                    coinCode = 'NST'
                AND hotMoney = 0
                GROUP BY
                    customerId
                HAVING
                    count(customerId) > 1
            )
        ) tab1
)
AND id NOT IN (
    SELECT
        min_id
    FROM
        (
            SELECT
                min(id) min_id
            FROM
                ex_digitalmoney_account
            WHERE
                coinCode = 'NST'
            AND hotMoney = 0
            GROUP BY
                customerId
            HAVING
                count(customerId) > 1
        ) tab2
)

 

你可能感兴趣的:(【笔记】MySQL删除重复记录保留一条)