刪除數據庫表中的重復記錄
阿新 • • 發佈:2018-08-02
col 編號 mys type gui where 刪除 code span
oracle
select * from gcfr_t_vch_delete E WHERE E.rowid < (SELECT max(X.rowid) FROM gcfr_t_vch_delete X WHERE X.vchno = E.vchno and e.type = x.type and e.curragency = x.curragency); --或者 select * from gcfr_t_vch_delete E WHERE E.rowid > (SELECT min(X.rowid) FROM gcfr_t_vch_delete X WHERE X.vchno =E.vchno and e.type = x.type and e.curragency = x.curragency);
以憑證的類型,編號,單位為判斷重復的依據。刪除重復的就是要刪除多余的並且只留下一條。上述sql的意思就是查出重復的記錄之後,以一個最大值為標準,把小於他的都刪掉。或者以最小的為標準,把大於他的都刪掉。這樣就正好滿足需求。
oracle利用rowid來保證數據的唯一性,其實用一個表字段也可以,但是要保證這個字段的唯一性,最好是主鍵。
mysql
select * from gcfr_t_vch_delete E WHERE E.guid < (SELECT max(X.guid) FROM gcfr_t_vch_delete X WHERE X.vchno = E.vchno and e.type = x.type and e.curragency = x.curragency); --或者 select * from gcfr_t_vch_delete E WHERE E.guid > (SELECT min(X.guid) FROM gcfr_t_vch_delete X WHERE X.vchno = E.vchno and e.type = x.type and e.curragency = x.curragency);
mysql這裏沒有rowid,以guid為例,guid是表的主鍵。
刪除數據庫表中的重復記錄