oracle——数据表的数据查询——oracle中的特殊表达式between and、in、like、is null、exists、all、some、any等
sql查询 create table yuangong ( id number, name varchar2(50), zhiwei varchar2(50), age number, sex varchar2(20) ) tablespace test insert into yuangong values(1,'周磊01','操作工',21,'男'); insert into yuangong values(2,'周磊02','操作工',23,'女'); insert into yuangong values(3,'周磊03','操作工',22,'男'); insert into yuangong values(4,'周磊04','操作工',24,'女'); insert into yuangong values(5,'周磊05','操作工',26,'男'); insert into yuangong values(6,'周磊06','操作工',25,'女'); insert into yuangong values(7,'周磊07','操作工',29,'男'); insert into yuangong values(8,'周磊08','操作工',28,'女'); insert into yuangong values(9,'周磊09','操作工',27,'男'); insert into yuangong values(10,'周磊10','操作工',25,'女'); insert into yuangong values(11,'周11磊','头目',39,'男'); insert into yuangong values(12,'周12磊','头目',31,'女'); insert into yuangong values(13,'周13磊','头目',37,'男'); insert into yuangong values(14,'周14磊','头目',34,'女'); insert into yuangong values(15,'周15磊','经理',45,'男'); select * from yuangong; drop table yuangong; ====================================================================== create table xinzi ( id number, yuangonghao number, yuefen number, jine number ) tablespace test select * from xinzi; insert into xinzi values(1,1,1,3800); insert into xinzi values(2,1,2,3600); insert into xinzi values(3,1,3,3300); insert into xinzi values(4,1,4,3200); insert into xinzi values(5,2,1,2800); insert into xinzi values(6,2,2,2600); insert into xinzi values(7,2,3,2300); insert into xinzi values(8,2,4,2200); insert into xinzi values(9,3,1,2200); insert into xinzi values(10,3,2,2500); insert into xinzi values(11,3,3,2300); insert into xinzi values(12,3,4,2800); insert into xinzi values(13,4,1,1200); insert into xinzi values(14,4,2,1500); insert into xinzi values(15,4,3,1300); insert into xinzi values(16,4,4,1800); --------------------------------------------------- insert into xinzi values(17,11,1,4200); insert into xinzi values(18,11,2,4500); insert into xinzi values(19,11,3,4300); insert into xinzi values(20,11,4,4800); insert into xinzi values(21,12,1,4800); insert into xinzi values(22,12,2,4600); insert into xinzi values(23,12,3,4300); insert into xinzi values(24,12,4,4200); insert into xinzi values(25,13,1,4200); insert into xinzi values(26,13,2,4300); insert into xinzi values(27,13,3,4400); insert into xinzi values(28,13,4,4500); insert into xinzi values(29,13,1,4200); insert into xinzi values(30,13,2,4300); insert into xinzi values(31,13,3,4400); insert into xinzi values(32,13,4,4500); ------------------------------------------------------- insert into xinzi values(33,15,1,7300); insert into xinzi values(34,15,2,7700); insert into xinzi values(35,15,3,7500); insert into xinzi values(36,15,4,7900); ======================================================================================= select * from yuangong; select * from xinzi; ===========================================================================================
oracle中的特殊表达式
between:取值范围 in:集合成员测试 like:模式匹配 is null:空值判断 all,some,any:数量判断 exists:存在性判断
select * from yuangong; select * from xinzi;
(1)between:取值范围 select * from yuangong where id between 2 and 5; select * from yuangong where id not between 2 and 5; select * from yuangong where age between 22 and 35; select * from yuangong where age not between 22 and 35;
(2)in:集合成员测试 select * from yuangong where id in (2,3,4,5); select * from yuangong where id not in (2,3,4,5);
(3)like:模式匹配 select * from yuangong where zhiwei like '头目'; select * from yuangong where zhiwei not like '头目'; select * from yuangong where name like '%磊';
(4)is null:空值判断 select * from yuangong; select * from xinzi; select * from yuangong where name is null; select * from yuangong where name is not null;
(5)all,some,any:数量判断 select * from xinzi where yuangonghao = 3 or yuangonghao = 4; select * from xinzi where jine > all ( select distinct jine from xinzi where yuangonghao = 3 or yuangonghao = 4 ); 此时这里all判断式相当于and运算,因为子查询用了distinct关键字,所以条件相当于 jine>2800 and jine>1800 select * from xinzi where jine > any ( select distinct jine from xinzi where yuangonghao = 3 or yuangonghao = 4 ); select * from xinzi where jine > some( select distinct jine from xinzi where yuangonghao = 3 or yuangonghao = 4 ); 此时,some相当于or运算,因为子查询用了distinct关键字,所以条件相当于 jine>2800 or jine>1800
(6)exists:存在性判断 in判断式用于判断表的列值是否存在于列表或者集合中; 而exists判断式可用于判断查询结果集是否为空; select * from yuangong where exists ( select * from xinzi where xinzi.id = yuangong.id );