子查询基本概念子查询,是指以结果集的形式供其他查询语句使用的查询语句。这样,可以在一个完整的查询语句之中,嵌套了若干个不同功能的小查询,从而一起完成更为复杂的查询。 根据结果集的类型不同,子查询大致可以分为如下四类:
单行单列子查询位置:select子句、where子句等可以使用一个常规值的任意子句。 select子句 功能:结果集作为一个常规值,参与字段列计算。 select ename, sal, (select avg(sal) from emp) as avg, (sal-(select avg(sal) from emp)) as balancefrom emp; where子句 功能:结果集作为一个常规值,参与查询条件计算(算数 逻辑)。
单行多列子查询位置:where子句。 功能:结果集作为一个复合值,参与查询条件的多个字段的判等计算。 select *from emp ewhere (e.job, e.sal) = (select job, sal from emp where ename='scott' ); 多行单列子查询位置:where子句。 功能:结果集作为一个集合值,参与查询条件的集合运算。
# any 集合运算符:可以不在集合中,满足任意一个比较条件。select *from empwhere sal > any(select min(sal) from emp where job='manager' group by deptno);
# exists 集合运算符:只检测结果集是否为空,不比较其中的元素。select *from emp ewhere exists(select * from emp); 多行多列子查询位置:from子句。 from子句 功能:结果集作为一个临时表,进行二次查询、多表连接等运算。
# 作为临时表进行多表连接;select e.*, d.dname, d.loc from emp e right join (select * from dept) d on e.deptno=d.deptno; insert子句 功能:采用【insert...select...】结构,取代【insert...values...】结构,实现从其他数据表导入数据的功能。
|
|
来自: Fengsq501u81r4 > 《计算机》