伪列
Oracle 中伪列就像一个表列(表中的列),但是它并没有存储在表中,伪列可以从表中查询,但不能插入、更新和删除它们的值,在Oracle中我们常见的伪列有两个分别为rownum 和 rowid,它实在我们的表生成之后才添加的,也就是说当select映射之后,才会生成,那么当 有语句的执行顺序在select之后时rownum的顺序会随着排序改变顺序
Oracle中如果要查询某张表中多个字段,又只对某个字段去重的时候用distinct或者group by都不行。distinct和group by会对要查询的字段一起进行去重,也就是当查询的所有字段都相同,oracle才认为是重复的。这时用rowid是个不错的选择。如下,有一张mytest表,中有id,naem,age字段,如果只想对name和age 字段进行去重并且保留id字段就可以使用下面的代码
create table mytest(
id int primary key not null,
name varchar(10) not null,
age int not null
);
insert into mytest values ('1','zs','40');
insert into mytest values ('2','zs','40');
insert into mytest values ('3','ls','30');
insert into mytest values ('4','ww','25');
insert into mytest values ('5','ls','30');
commit;
-- 使用rowid去重的代码 即内外表关联
select id,name,age from mytest t1
where t1.rowid in (
select max(t2.rowid) from mytest t2 where t1.name= t2.name and t1.age=t2.age);
t1和t2中rowid是一个字段,因为t1和t2是一张表
原理实际上是test表以name和age字段进行自查询,对于name和age中没有重复的数据rowid只有一个值,所以max/mian(rowid)就是rowid本身,但是对于name和age中重复的数据rowid会有多个,取max/min的一个,这样就去重了,由于rowid (伪列,数据的物理地址信息)所以此方法是是通过文件去操作数据,效率是最高的。