- 分组查询,查到数据大于1的有哪些数据
- 查这些重复数据最小的id是什么(如果没有这个,那此方法不适用)
- 关联查询,条件是值相同,但是id大于最小id,最后在进行删除。
实际使用SQL如下:
SELECT vim_id, vim_name,count(*)
FROM ops_sys_vgw_info
group by vim_id,vim_name
having count(*) > 1;
SELECT vim_id, vim_name,min(id)
FROM ops_sys_vgw_info
group by vim_id,vim_name
having count(*) > 1;
DELETE a
FROM ops_sys_vgw_info a, (
SELECT vim_id, vim_name, min(id) AS id
FROM ops_sys_vgw_info
GROUP BY vim_id, vim_name
HAVING count(*) > 1
) b
WHERE a.vim_id = b.vim_id
AND a.id > b.id;
参考:https://blog.csdn.net/weixin_40052298/article/details/118415616
评论