复杂查询(连接查询、子查询)
2017-10-11 23:23
190 查看
-- 二、复杂的查询:
/*
1. 连接查询
1.1内连接
select 字段列表 from 表1,表2,……
where 表1.列=表2.列 and ……
或
select 字段列表 from 表1 join 表2
on 表1.列=表2.列 join 表3
on……
*/
-- 显示smith这位员工的信息以及他所在部门的信息
select * from emp,dept
where emp.deptno = dept.deptno and ename='SMITH';
select * from emp join dept
on emp.deptno=dept.deptno and ename='SMITH' ;
-- 显示每位员工的上级领导的姓名------自连接
select a.ename 职员,b.ename 领导 from emp a,emp b
where a.mgr=b.empno;
select * from emp,dept-----内连接:将两张表相匹配的显示出来,不匹配的不显示
where emp.deptno = dept.deptno ;
-- 1.2外连接
/*
(1)右向外连接
select 字段列表 from 表1 right outer join 表2
on 表1.列=表2.列
或:
select 字段列表 from 表1,表2,……
where 表1.列(+)=表2.列 and……
*/
select * from emp,dept-----右向外连接:右表全部显示,左表之显示匹配的
where emp.deptno (+)= dept.deptno;
select * from emp right outer join dept
on emp.deptno = dept.deptno;
/*
(2)左向外连接
select 字段列表 from 表1 left outer join 表2
on 表1.列=表2.列
或;
select 字段列表 from 表1,表2,……
where 表1.列=表2.列 and……(+)
*/
select * from emp,dept-----左向外连接:左表全部显示,右表之显示匹配的
where dept.deptno = emp.deptno(+);
select * from dept left outer join emp
on emp.deptno = dept.deptno;
/*
(3)完全外连接
select 字段列表 from 表1 full join 表2
on 表1.列=表2.列
*/
select * from emp full join dept
on emp.deptno = dept.deptno;--完整外部联接返回左表和右表中的所有行。
--当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。
--如果表之间有匹配行,则整个结果集行包含基表的数据值。
--2.子查询
/*
2.1、非关联的子查询
特点:先由子查询得到一个结果,
主查询根据子查询得到的结果进行的查询
*/
--1)用关系运算符实现子查询(> >= < <= = != <>)
--特点:子查询得到的结果只有一个值
--例如:显示高于平均工资的员工信息
select * from emp
where sal>(select avg(sal)from emp);
--2)用in实现的子查询
--特点:子查询得到的结果可以多个值
--例如:显示在NEW YORK工作的员工信息
select * from emp
where deptno in(select deptno from dept
where loc='NEW YORK');
/*
2.2、关联的子查询
特点:将主查询、子查询作为条件共同查询
*/
--例如:查询每个部门最高工资的员工信息
--(1)用exists实现的子查询
select * from emp
where exists
(
select * from (
select deptno,max(sal)maxSal from emp
group by deptno )temp
where emp.deptno=temp.deptno and emp.sal=temp.maxSal
);
--(2)子查询相当于内循环
select * from emp a
where (select count(empno) from emp b
where a.deptno=b.deptno and b.sal>a.sal)=0;
/*
1. 连接查询
1.1内连接
select 字段列表 from 表1,表2,……
where 表1.列=表2.列 and ……
或
select 字段列表 from 表1 join 表2
on 表1.列=表2.列 join 表3
on……
*/
-- 显示smith这位员工的信息以及他所在部门的信息
select * from emp,dept
where emp.deptno = dept.deptno and ename='SMITH';
select * from emp join dept
on emp.deptno=dept.deptno and ename='SMITH' ;
-- 显示每位员工的上级领导的姓名------自连接
select a.ename 职员,b.ename 领导 from emp a,emp b
where a.mgr=b.empno;
select * from emp,dept-----内连接:将两张表相匹配的显示出来,不匹配的不显示
where emp.deptno = dept.deptno ;
-- 1.2外连接
/*
(1)右向外连接
select 字段列表 from 表1 right outer join 表2
on 表1.列=表2.列
或:
select 字段列表 from 表1,表2,……
where 表1.列(+)=表2.列 and……
*/
select * from emp,dept-----右向外连接:右表全部显示,左表之显示匹配的
where emp.deptno (+)= dept.deptno;
select * from emp right outer join dept
on emp.deptno = dept.deptno;
/*
(2)左向外连接
select 字段列表 from 表1 left outer join 表2
on 表1.列=表2.列
或;
select 字段列表 from 表1,表2,……
where 表1.列=表2.列 and……(+)
*/
select * from emp,dept-----左向外连接:左表全部显示,右表之显示匹配的
where dept.deptno = emp.deptno(+);
select * from dept left outer join emp
on emp.deptno = dept.deptno;
/*
(3)完全外连接
select 字段列表 from 表1 full join 表2
on 表1.列=表2.列
*/
select * from emp full join dept
on emp.deptno = dept.deptno;--完整外部联接返回左表和右表中的所有行。
--当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。
--如果表之间有匹配行,则整个结果集行包含基表的数据值。
--2.子查询
/*
2.1、非关联的子查询
特点:先由子查询得到一个结果,
主查询根据子查询得到的结果进行的查询
*/
--1)用关系运算符实现子查询(> >= < <= = != <>)
--特点:子查询得到的结果只有一个值
--例如:显示高于平均工资的员工信息
select * from emp
where sal>(select avg(sal)from emp);
--2)用in实现的子查询
--特点:子查询得到的结果可以多个值
--例如:显示在NEW YORK工作的员工信息
select * from emp
where deptno in(select deptno from dept
where loc='NEW YORK');
/*
2.2、关联的子查询
特点:将主查询、子查询作为条件共同查询
*/
--例如:查询每个部门最高工资的员工信息
--(1)用exists实现的子查询
select * from emp
where exists
(
select * from (
select deptno,max(sal)maxSal from emp
group by deptno )temp
where emp.deptno=temp.deptno and emp.sal=temp.maxSal
);
--(2)子查询相当于内循环
select * from emp a
where (select count(empno) from emp b
where a.deptno=b.deptno and b.sal>a.sal)=0;
相关文章推荐
- ORACLE复杂查询之连接查询
- 复杂的左连接查询
- 15.数据库原理 事务的原理,锁机制,表连接,复杂查询语句(工作经验),性能调优,锁表以及解决方案==
- C#连接SQL多条件复杂查询
- SQL SELECT(复杂查询) 之 等值查询 内外连接
- SQL SELECT(复杂查询)之 自连接 & 子查询 解析
- SQL语句:一种连接本表2个查询结果的复杂sql语句、一种查找一列有重复值的语句
- 用SQL语言进行复杂查询:对各表中的数据进行不同条件的连接查询和嵌套查询: 1)查询每个学生及其选课情况; 2)查询每门课的间接先修课
- ORACLE复杂查询之连接查询
- MSSQL分页存储过程,支持连接查询等复杂的查询
- 表的复杂查询----多表查询、两表的条件连接、内连接(自连接)、左外连接、右外连接、子查询、分页查询
- ORACLE复杂查询之连接查询
- SQL常用的几种连接查询
- 关于多表连接查询的使用环境,以及和聚合函数的配合使用限制
- sql连接一对多查询获取一对一
- JPA Predicate复杂查询排序
- LINQ To XML:之复杂的查询举例
- sql server2008 批量删除发生:查询处理器用尽了内部资源,无法生成查询计划。这种情况很少出现,只有在查询极其复杂或引用了大量表或分区时才会出现。请简化查询
- SQL多表连接查询(详细实例)
- 022——hibernate连接查询