Oracle学习笔记——基础一起学 16

本文介绍了在OracleSQL中删除student表中重复记录的四种方法,包括利用rowid、自连接、嵌套查询以及groupingsets进行分组汇总。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--删除重复记录

--创建student表

create table student(sno number(6) ,sname varchar2(10),sage int);

insert into student values(1,'AA',21);

insert into student values(2,'BB',22);

insert into student values(3,'CC',23);

insert into student values(3,'CC',34);

insert into student values(3,'CC',35);

insert into student values(3,'CC',36);

--删除重复的记录,第一种方法,rowid

--先查看表的rowid,rowid里的信息有:数据库对象号、数据文件号、数据块号、行号。作用唯一标识一行。

--查询rowid是运行最快的方式。

select student.* ,rowid from student;

--第一种方式,删除sno重复并且保证rowid不是最小的

delete student where sno in(select sno from student group by sno having count(*)>1)

and rowid not in

(select min(rowid) from student group by sno having count(*)>1);

--第二种方式,自连接(筛选左表rowid>右边rowid的记录)

select a.sno,a.sname,a.sage,b.sno,b.sname,b.sage from student a,student b where a.sno=b.sno;

delete from student where rowid in (select a.rowid from student a,student b where a.sno=b.sno and a.rowid>b.rowid);

--第三种,嵌套查询(删除rowid>最小rowid的集)

delete from student d where d.rowid>

(select min(rowid) from student x where d.sno=x.sno);

--第四种,嵌套查询,找出学号为3,年龄大于23的)

delete (select * from (select * from student where sno=3) where sage>23);

select * from student;

--group by grouping sets的使用

/*可以用group by grouping sets来进行分组自定义汇总,可以用它来指定你需要的总数组合

其格式为  group by grouping sets ((list),(list)...) 这里的(list) 是圆括号中的一个列序列,

这个组合生成一个总数。要增加一个总和,必须增加一个(null)分组集。

*/

如果能帮到你请感谢我的老婆“一行琉璃”

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值