隨筆-22  評論-6  文章-17  trackbacks-0

          幾個刪除重復記錄的SQL語句

          在大的數據庫應用中,經常因為各種原因遇到重復的記錄,造成數據的冗余和維護上的不便。

          1.用rowid方法

          2.用group by方法

          3.用distinct方法

          1。用rowid方法

          據據oracle帶的rowid屬性,進行判斷,是否存在重復,語句如下:
          查數據:
              select * from table1 a where rowid !=(select  max(rowid) 
              from table1 b where a.name1=b.name1 and a.name2=b.name2......)
          刪數據:
             delete  from table1 a where rowid !=(select  max(rowid) 
              from table1 b where a.name1=b.name1 and a.name2=b.name2......)

          2.group by方法

          查數據:

            select count(num), max(name) from student --列出重復的記錄數,并列出他的name屬性
            group by num
            having count(num) >1 --按num分組后找出表中num列重復,即出現次數大于一次
          刪數據:
            delete
          from student
            group by num
            having count(num) >1

            這樣的話就把所有重復的都刪除了。

          3.用distinct方法 -對于小的表比較有用

          create table table_new as  select distinct *  from table1 minux
          truncate table table1;
          insert into table1 select * from table_new;

          select sum(bag_weight),sum(bag_total) from tdespatch
          posted on 2005-10-25 13:58 surffish 閱讀(715) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 河北区| 孙吴县| 江都市| 紫金县| 巢湖市| 共和县| 鄂伦春自治旗| 泽州县| 普兰店市| 安龙县| 开江县| 八宿县| 渭南市| 射洪县| 翁源县| 泗水县| 鄱阳县| 玉环县| 平顺县| 赤水市| 古丈县| 包头市| 平昌县| 揭东县| 高碑店市| 米泉市| 定陶县| 轮台县| 德州市| 永德县| 临江市| 漳平市| 西畴县| 肥乡县| 宁安市| 谷城县| 裕民县| 临泽县| 湘西| 临西县| 吉木萨尔县|