您的位置:首页 > 数据库 > Oracle

【Oracle】模糊查询与高级连接查询

2016-11-02 22:21 405 查看

区间和范围查询

Between … and 的用法

//查询薪水在2000-3000之间的员工信息
select * from emp where sal>=2000 and sal<=3000l
or
select * from emp where sal 2000 and 3000;


is null(空字符串)

//查询没有上级的员工信息
select * from emp where mgr is null;


模糊查询 (like ‘%关键字%’)

//查询薪水高于2000或者岗位为CLERK的员工信息,同时还要满足其姓名首字母为'J'的信息
select * from emp where (sal>2000 or job='CLERK') and ename like'J%';


高级连接查询

内连接查询

//内连接查询(返回两张表共同的数据)
select * from 表1 inner join 表2 on 表1.公共列 = 表2.公共列;


左外连接查询

select * from 表1 left join 表2 on 表1.公共列 = 表2.公共列;


右外连接查询

select * from 表1 right join 表2 on 表1.公共列 = 表2.公共列;


完全连接查询

select * from 表1 full join 表2 on 表1.公共列 = 表2.公共列;


交叉连接(笛卡尔积)

select * from 表1,表2 where 表1.公共列 = 表2.公共列;


自连接查询(自己查询自己)

//查询员工‘SMITH’的上级领导姓名

//方式一 子查询
select * from emp where empno = (select mgr from emp where ename='SMITH');

//方式二 一个查询结果作为一个新表
select * from (select mgr from emp where ename='SMITH') 上级表 inner join emp on 上级表.mgr = emp.empno;

//方式三
select * from emp,(select mgr from emp where ename='SMITH') 上级表 where emp.empno = 上级表.mgr;


子查询(在SQL语句中嵌套的查询)

单行子查询

//单行子查询:返回一个值的子查询 = > < >= <=

//查询与‘SMITH’同部门的员工
select * from emp where deptno = (select deptno from emp      where emp.ename='SMITH');


多行子查询 (in / not in)

//查询与部门10号员工同岗位的员工
select * from emp where job in (select job from emp where deptno=10)


多列子查询

//查询与'SMITH'同部门岗位的员工
select * from emp where (deptno.job) = (select deptno,job from emp where ename='SMITH');


内联视图子查询

//内联视图子查询:将子查询的结果作为另一个查询的数据源
//查询薪水最低的5个员工的信息
//注:因为rownum是先序列再排序,不能直接select * from 表名 order   //by列 where rownum<5,所以先把排序的结果集在rownum序列一次

select  rownum,t1.* from (select emp.* from emp order by sal asc) t1 where rownum<=5;

//查询员工信息表1-5行的数据
select t1.* from (select rownum r,emp.* from emp where rownum <=10) t1 where r>5 and r<=10;


分页查询

//每页有多少行(PageCount),当前第几页(Page)
select t1.* from (select rownum r,emp.* from where rownum<=pagecount*page) t1 where r>pagecount*(page-1) and r<=pagecount*page;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: