删除数据库重复的记录

如果数据库中有重复数据,则对重复的数据保留一条,其他删出,以交易日期表举例,关联字段为唯一索引字段。

one:

delete exchangedate a
 where exists(select 1
                from (select *
                        from (select init_date,
                                     finance_type,
                                     exchange_type,
                                     min(rowid) as row_id,
                                     count(*) as row_count
                                from exchangedate
                            group by init_date, finance_type, exchange_type)
                       where row_count > 1) b
               where a.finance_type = b.finance_type
                 and a.exchange_type = b.exchange_type
                 and a.init_date = b.init_date
                 and a.rowid <> b.row_id);

two:

delete exchangedate a
 where a.rowid > (select min(rowid) 
                    from exchangedate b 
                   where a.finance_type = b.finance_type
                     and a.exchange_type = b.exchange_type
                     and a.init_date = b.init_date);

扩展:按某个字段分组,然后将这个字段下数据只保留一条记录。

delete hs_user.functiontomenu a
where exists(select 1 
               from (select menu_id,min(rowid) as row_id
                       from hs_user.functiontomenu
                   group by menu_id) b
              where a.menu_id = b.menu_id
                and a.rowid > b.row_id);

 

转载于:https://my.oschina.net/u/2332532/blog/691245

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值