sql删除重复记录只保留一条
xuexi 2025-01-07 10:03:18 发表在:PHP 查看数:361

查询重复记录

select 字段名 from 表名 where 字段名 in (
    select 字段名  from 表名 group by 字段名  having count(1) >= 2
) ORDER BY  字段名

查询出所有重复记录并且删除多余的只保留一条

delete from 表名
where 
重复字段名 in (
    SELECT a.重复字段名from(
    select 重复字段名
    from 表名
    group by 重复字段名 having count(1) > 1
    ) a
)
and 
id(只保留id最小的一个) not in (
SELECT b.id from(
    select min(id) as id
    from 表名 
    group by 重复字段名 having count(1)>1
    ) b
)
最近访问时间:2025-03-25 14:19:49
知识库:331条鸣谢:TAY  备案号:蜀ICP备2024090044号