如何删除重复的行并保留第一行?

如何删除重复的行并保留第一行?,第1张

如何删除重复的行并保留第一行? 备份数据,然后…

MySQL在DELETe语句中支持JOIN。如果要保留第一个重复项:

DELETE a  FROM MYVIEWS a  JOIN (SELECt MIN(t.a1) AS min_a1, t.k1, t.k2, t.k3          FROM MYVIEWS t      GROUP BY t.k1, t.k2, t.k3        HAVINg COUNT(*) > 1) b ON b.k1 = a.k1        AND b.k2 = a.k2        AND b.k3 = a.k3        AND b.min_a1 != a.a1

如果要保留最后一个重复项:

DELETe a  FROM MYVIEWS a  JOIN (SELECt MAX(t.a1) AS max_a1, t.k1, t.k2, t.k3          FROM MYVIEWS t      GROUP BY t.k1, t.k2, t.k3        HAVINg COUNT(*) > 1) b ON b.k1 = a.k1        AND b.k2 = a.k2        AND b.k3 = a.k3        AND b.max_a1 != a.a1


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4976425.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存