MySQL中删除重复数据只保留一条

DELETE FROM brand_group_relation
WHERE id IN (
        SELECT id
        FROM (
            SELECT id
            FROM brand_group_relation
            WHERE (brand_group_id, brand_value_id) IN (
                    SELECT brand_group_id, brand_value_id
                    FROM brand_group_relation
                    GROUP BY brand_group_id, brand_value_id
                    HAVING COUNT(*) > 1
                )
                AND id NOT IN (
                    SELECT MIN(id)
                    FROM brand_group_relation
                    GROUP BY brand_group_id, brand_value_id
                    HAVING COUNT(*) > 1
                )
            ORDER BY brand_group_id, brand_value_id
        ) tmp
    );

参考

你可能感兴趣的:(MySQL中删除重复数据只保留一条)