根据字段删除重复的记录保留最新数据

日期:2019-05-12 浏览:201次

表(order_test)结构:

mysql 语句:

DELETE FROM order_test WHERE
(
name IN ( SELECT n.name FROM
( SELECT name FROM order_test GROUP BY name HAVING COUNT(name) > 1 ) n )
) AND (
id NOT IN (
SELECT i.id FROM
( SELECT max(id) AS id FROM order_test GROUP BY name HAVING COUNT(name) > 1 ) i
)
)

执行结果:

eg:这里用的max(id),保留最新数据。保留最开始的数据采用min(id)

没有评论

发表评论