mysql 删除重复记录语句

时间:2023-03-08 15:59:15

mysql 根据条件删除重复记录 只保留最小id的重复数据

DELETE
FROM
news
WHERE
news_id IN (
SELECT
a.news_id
FROM
(
SELECT
news_id
FROM
news
WHERE
flag = 1
GROUP BY
news_id
HAVING
count(news_id) > 1
) a
)
AND posid NOT IN (
SELECT
b.posid from (
SELECT
min(posid) as posid
FROM
news
where flag=1
GROUP BY
news_id
HAVING
count(news_id) > 1
) b
)