刪除數據庫重複的記錄

若是數據庫中有重複數據,則對重複的數據保留一條,其餘刪出,以交易日期表舉例,關聯字段爲惟一索引字段。sql

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:code

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);
相關文章
相關標籤/搜索