隨筆-22  評(píng)論-6  文章-17  trackbacks-0

          幾個(gè)刪除重復(fù)記錄的SQL語(yǔ)句

          在大的數(shù)據(jù)庫(kù)應(yīng)用中,經(jīng)常因?yàn)楦鞣N原因遇到重復(fù)的記錄,造成數(shù)據(jù)的冗余和維護(hù)上的不便。

          1.用rowid方法

          2.用group by方法

          3.用distinct方法

          1。用rowid方法

          據(jù)據(jù)oracle帶的rowid屬性,進(jìn)行判斷,是否存在重復(fù),語(yǔ)句如下:
          查數(shù)據(jù):
              select * from table1 a where rowid !=(select  max(rowid) 
              from table1 b where a.name1=b.name1 and a.name2=b.name2......)
          刪數(shù)據(jù):
             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方法

          查數(shù)據(jù):

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

            這樣的話就把所有重復(fù)的都刪除了。

          3.用distinct方法 -對(duì)于小的表比較有用

          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 閱讀(714) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 忻城县| 嘉定区| 兰溪市| 三明市| 文山县| 灌阳县| 如东县| 五原县| 沈阳市| 鸡泽县| 高雄市| 泸水县| 浦县| 兴安盟| 石渠县| 仁化县| 嘉义县| 万山特区| 久治县| 平远县| 金华市| 谢通门县| 左云县| 绍兴市| 红原县| 黔东| 澜沧| 依安县| 乌拉特后旗| 台江县| 四川省| 贵南县| 尚志市| 广水市| 邻水| 阜康市| 元阳县| 宜州市| 寻乌县| 滦南县| 塔城市|