ID 姓名 分数 课程
1 jiangjun 60 语文
2 jiangjun 60 语文
3 jiangjun 70 数学
4 wangjian 60 语文
5 wangjian 40 数学
6 weicao 50 语文
7 weicao 70 英语
8 weicao 60 数学写2个SQL语句.
1.删除表中除了ID 都重复的项.
2.列出每门成绩都大于60的学生的姓名
1 jiangjun 60 语文
2 jiangjun 60 语文
3 jiangjun 70 数学
4 wangjian 60 语文
5 wangjian 40 数学
6 weicao 50 语文
7 weicao 70 英语
8 weicao 60 数学写2个SQL语句.
1.删除表中除了ID 都重复的项.
2.列出每门成绩都大于60的学生的姓名
creat table tbl_tmp (select distinct* from tbl);truncate table tbl;//清空表记录insert into tbl select * from tbl_tmp;//将临时表中的数据插回来。这种方法可以实现需求,但是很明显,对于一个千万级记录的表,这种方法很慢,在生产系统中,这会给系统带来很大的开销,不可行。2、利用rowid在oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,rowid确定了每条记录是oracle中的哪一个数据文件、块、行上。在重复的记录中,可能所有列的内容都相同,但rowid不会相同。SQL语句如下:
delete from tbl where rowid in (select a.rowid from tbl a, tbl b where a.rowid>b.rowid and a.col1=b.col1 and a.col2 = b.col2)如果已经知道每条记录只有一条重复的,这个sql语句适用。但是如果每条记录的重复记录有N条,这个N是未知的,就要考虑适用下面这种方法了。3、利用max或min函数这里也要使用rowid,与上面不同的是结合max或min函数来实现。SQL语句如下
delete from tbl awhere rowid not in (select max(b.rowid) from tbl b where a.col1=b.col1 and a.col2 = b.col2);//这里max使用min也可以或者用下面的语句
delete from tbl awhere rowid<(select max(b.rowid) from tbl b where a.col1=b.col1 and a.col2 = b.col2);//这里如果把max换成min的话,前面的where子句中需要把"<"改为">"跟上面的方法思路基本是一样的,不过使用了group by,减少了显性的比较条件,提高效率。SQL语句如下:
deletefrom tbl where rowid not in (select max(rowid) from tbl tgroup by t.col1, t.col2);delete from tbl where (col1, col2) in (select col1,col2 from tblgroup bycol1,col2havingcount(*) >1)and rowidnotin(selectnin(rowid)fromtblgroup bycol1,col2havingcount(*) >1)还有一种方法,对于表中有重复记录的记录比较少的,并且有索引的情况,比较适用。假定col1,col2上有索引,并且tbl表中有重复记录的记录比较少,SQL语句如下4、利用group by,提高效率
select distinct name
from score t
where not exists
(select 1 from score v where v.name=t.name and v.score<60)
千万级 数据用你的第二种方法会 会更慢,这样大的数据量,用大于或小于,慢的要命!!!!!!!!!
如果原表不是很大的话,建议用以下语句建一个新表(新表中没有原表中除了ID外重复的行),然后再覆盖原表(此办法优点是可以很容易实现,缺点是丢失ID,需要自己再加上ID列,并用序列自动编号):
create table stu_new as select distinct 姓名,分数,课程 from 原表名;
2:
select distinct 学生姓名 from 成绩表 where 学生姓名 not in (select distinct 学生姓名 from 成绩表 where 分数<60);
delete from table_name t
where t.rowid>(select min(t1.rowid)
from table_name t1
where t1.姓名=t.姓名
and t1.分数=t.分数
and t1.课程=t.课程)
create table student_test(
id number,
name varchar2(10),
score number,
course varchar2(10)
);
insert into student_test values(1,'jiangjun',60,'语文');
insert into student_test values(2,'jiangjun',60,'语文');
insert into student_test values(3,'jiangjun',70,'数学');
insert into student_test values(4,'wangjian',60,'语文');
insert into student_test values(5,'wangjian',40,'数学');
insert into student_test values(6,'weicao',50,'语文');
insert into student_test values(7,'weicao',70,'英语');
insert into student_test values(8,'weicao',60,'数学');
commit;
1-------------------- delete from student_test
where id = (select c.id
from (select s.*,
row_number() over(partition by s.name, s.score, s.course order by id) rn
from student_test s) c
where rn <> 1);2---------------------------- select c.name from (
select s.name,min(s.score) min_score
from student_test s
group by s.name
)c
where c.min_score > 60;
270606112 谢谢
1:
delete from table_name t
where t.rowid>(select min(t1.rowid)
from table_name t1
where t1.姓名=t.姓名
and t1.分数=t.分数
and t1.课程=t.课程)
这个不错,跟临时表比,性能哪个更好一些呢?