posts - 60,comments - 71,trackbacks - 0
   
     在一些数据表中,可能会有一些重复(某一字段,或全部字段)的数据,根据需求,可能要去掉这些重复的数据,下面是常用的方法:
--表 test有重复的记录1,10
  SQL> select  *  from tt;
   ID
  ----------
   1
   2
   3
   4
   10
   1
   1
   1
   1
   1
   10
  11 rows selected.

  --查询表中的哪些记录有重复
  SQL> select * from tt  group by id having count(*)>1;
   ID
  ----------
   1
   10

  --查询出没有重复记录的结果集
  SQL> select * from tt  group by id;
   ID
  ----------
   1
   2
   3
   4
   10
  SQL> select distinct * from tt;
   ID
  ----------
   1
   2
   3
   4
   10

  --删除重复的记录
  SQL> delete from tt  a where a.rowid!=(select max(rowid) from tt  b
   2 where a.id=b.id);
  6 rows deleted.
  SQL> commit;
  Commit complete.

  --删除后的查询结果集
  SQL> select * from tt;
   ID
  ----------
   2
   3
  
  
   4
   1
   10
posted on 2009-06-12 10:56 henry1451 阅读(235) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: