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

--删除重复记录

--创建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. Oracle学习笔记——基础一起 16

    2024-01-20 22:38:02       51 阅读
  2. Oracle学习笔记——基础一起 15

    2024-01-20 22:38:02       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-20 22:38:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-20 22:38:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-20 22:38:02       82 阅读
  4. Python语言-面向对象

    2024-01-20 22:38:02       91 阅读

热门阅读

  1. python元类模型和class语句协议

    2024-01-20 22:38:02       66 阅读
  2. leetcode-路径总和

    2024-01-20 22:38:02       68 阅读
  3. C语言零基础入门(结构体)

    2024-01-20 22:38:02       53 阅读
  4. 解决Spring Boot应用打包后文件访问问题

    2024-01-20 22:38:02       70 阅读
  5. 多模态是什么意思,在生活工业中有哪些应用?

    2024-01-20 22:38:02       106 阅读
  6. Python Matplotlib 实现基础绘图

    2024-01-20 22:38:02       61 阅读
  7. 题记(18)--日志排序

    2024-01-20 22:38:02       63 阅读
  8. SpringBoot+Redisson分布式锁

    2024-01-20 22:38:02       61 阅读
  9. 如何在ubuntu18.04安装python3.8.6

    2024-01-20 22:38:02       59 阅读