Chinaunix首页 | 论坛 | 博客
  • 博客访问: 483661
  • 博文数量: 401
  • 博客积分: 244
  • 博客等级: 入伍新兵
  • 技术积分: 2215
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-04 10:02
文章分类

全部博文(401)

文章存档

2013年(37)

2012年(364)

分类:

2012-09-12 14:27:45

表记录如下:
SQL> select * from t1;

A               B
------ ----------
hh            102
wxc           101
wxc           101
wxc           101
wxc           101
hh            102
hh            102
hh            102
方法一:
create table t2 as (select distinct a,b from t1);
drop table t1;
create table t1 as select * from t2;

方法二:rowid方法
delete from t1 where rowid in (select a.rowid from t1 a,t1 b where a.a = b.a and a.b=b.b and a.rowid > b.rowid);

方法三:max,min函数方法
delete from t1 a where rowid not in(select max(b.rowid) from t1 b where b.a=a.a and b.b=a.b);
delete from t1 a where rowid < (select max(b.rowid) from t1 b where b.a=a.a and b.b=a.b);
delete from t1 a where rowid not in(select min(b.rowid) from t1 b where b.a=a.a and b.b=a.b);
delete from t1 a where rowid > (select min(b.rowid) from t1 b where b.a=a.a and b.b=a.b);

方法四:group by函数方法
delete from t1 where rowid not in(select max(rowid) from t1 group by a,b);
delete from t1 where rowid not in(select min(rowid) from t1 group by a,b);
delete from t1 where (a,b) in (select a,b from t1 group by a,b having(count(*)) > 1) and rowid not in (select max(rowid) from t1 group by a,b having count(*) > 1);
delete from t1 where (a,b) in (select a,b from t1 group by a,b having(count(*)) > 1) and rowid not in (select min(rowid) from t1 group by a,b having count(*) > 1);
阅读(152) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~