SQL查询 —— 去除多个字段相同的记录,只保留一个

背景:去重,去除多个字段相同的记录,保留一个

场景一:去除表中所有重复数据,均只保留一个

delete from tbl_dept where dept_id not in (
    select id from (
        select min(dept_id) as id
        from tbl_dept 
        group by dept_name,rm_flag
    ) as temp
)

场景二:只去除表中市声部的重复数据,且只保留一个

delete from tbl_dept where dept_id not in (
    select id from (
        select min(dept_id) as id
        from tbl_dept 
        group by dept_name,rm_flag
    ) as temp
) and dept_name like '%市声部%'

猜你喜欢

转载自www.cnblogs.com/yifanSJ/p/9215212.html
今日推荐