子查询

### 1\. 子查询的概念


##### 1.1 子查询的概念和理解


某些查询逻辑中,需要引入另一个查询做为条件或者数据来源进行辅助。

比如:查询与‘TOM’在同一部门工作的其他员工


```

select * from emp where deptno = TOM的deptno and

ename <> 'TOM';


```


TOM的deptno


```

select deptno from emp where ename = 'TOM';


```


将两个独立的查询合并起来


```

select * from emp

where deptno = (select deptno from emp

where ename = 'TOM')

and ename <> 'TOM';


```


整个外部这部分SQL语句被称为外部查询(主查询),括号内查询TOM部门号的这个SQL语句被称为子查询


![image](//upload-images.jianshu.io/upload_images/15606715-7f59de199e141853.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/579/format/webp)


##### 1.2 子查询使用位置


子查询可以使用在很多子句中


*  from子句中,from子句中的子查询可以理解为是一张临时的“表”(视图)

*  where子句中

*  having子句中

    通常主要使用在where和from中


### 2\. 简单子查询(单行子查询)


**单行子查询的查询结果是一行一列的数据**

可以使用常规的> ,<, >=, <=, =, !=进行子查询数据的比较

可以将子查询理解为就是一个简单的数据

示例1:查询比TOM月薪高的员工信息


```

select * from emp where sal > (select

sal from emp where ename = 'TOM')


```


![image](//upload-images.jianshu.io/upload_images/15606715-d95e5201e43012f4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/568/format/webp)


示例2:查询与LEE是同一职位同一部门的其他员工信息


```

select *

from emp

where job = (select job from emp where

ename = 'LEE')

and deptno = (select deptno from emp where

ename = 'LEE')

and ename <> 'LEE'


```


![image](//upload-images.jianshu.io/upload_images/15606715-c4c26cc14031ce13.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/572/format/webp)


**子查询中可以嵌套其他的子查询,层数限制在32层内**

示例3:查询月薪高于AMY所在部门平均月薪的员工信息


```

select *

from emp

where sal > (select AVG(sal) from emp

        where deptno = (select deptno from emp where ename = 'AMY'))


```


![image](//upload-images.jianshu.io/upload_images/15606715-e44b598a58799cf4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/580/format/webp)


### 3\. 多行子查询


##### 3.1 多行子查询


**多行子查询返回的是多行一列的数据**

当子查询返回多行记录时,使用=这类的比较运算符无法执行


![image](//upload-images.jianshu.io/upload_images/15606715-aa3a7c5ae957c27b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/764/format/webp)


当有多条记录返回时,使用IN来进行比较,相当于等于子查询中某个值即可

示例4:查询是经理的员工信息(=)


```

select *

from emp

where empno in (select distinct mgr from

emp where mgr is not null)


```


![image](//upload-images.jianshu.io/upload_images/15606715-3bc5ecc84a947548.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/573/format/webp)


示例5:查询不是经理的员工信息(!=)


```

select *

from emp

where empno not in (select distinct mgr

from emp where mgr is not null)


```


![image](//upload-images.jianshu.io/upload_images/15606715-284d3ad1a1ce336e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/577/format/webp)


**not in中的数据不能有null值,如果有null值,相当于执行了=null操作,不能筛选出任何数据**


##### 3.2 ANY(SOME)与ALL


主要使用在多行子查询中进行>或<与操作时

ANY(SOME):任何一个

ALL:所有


> ANY 比最小的大

> ALL 比最大的大

> < ANY 比最大的小

> < ALL 比最小的小


![image](//upload-images.jianshu.io/upload_images/15606715-c36b78957b6f97b0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/917/format/webp)


示例6:查询比10部门所有人月薪都要高的员工信息


```

select *

from emp

where sal > ALL(select sal from emp

where deptno = 10)


```


也可以使用


```

select *

from emp

where sal > (select MAX(sal) from emp

where deptno = 10)


```


![image](//upload-images.jianshu.io/upload_images/15606715-bdda039cfb58dbb8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/572/format/webp)


使用ANY和ALL执行效率要高于分组函数


##### 3.3 from子句中的子查询


将子查询运用在from中,相当于一张“表”

**必须为作为“表”的子查询起“表别名”(示例7中的t)**

示例7:查询部门编号,部门名称,部门loc,部门人数


```

select

d.deptno,d.dname,d.loc,IFNULL(t.empnum,0)

from dept d left join

   (select deptno, COUNT(empno) as empnum

   from emp

   where deptno is not null

   group by deptno) t

    on(d.deptno = t.deptno)


```


![image](//upload-images.jianshu.io/upload_images/15606715-4cbc9c71f321e8e8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/386/format/webp)


**在MySQL中,update语句分组函数的结果不能作为子查询的返回结果**


```

update emp set sal = sal + 200

where sal < (select avg(sal) from emp);


```


MySQL中认为更新和子查询不能同时进行。

解决办法:将子查询再次进行嵌套,制作成From中的子查询


```

update emp set sal = sal + 200

where sal < (select asal from(select

avg(sal) from emp) t);


```


### 4\. 相关子查询(难点)


前3节的子查询都属于**独立子查询**,子查询可以独立执行

相关子查询:子查询不可独立执行,必须依赖外部查询


##### 4.1 常规的相关子查询


示例8:查询比自己所在部门平均月薪高的员工信息

常规写法


```

select * from emp e

     join (select deptno, AVG(sal) as asal

       from emp

       group by deptno) t

     on(e.deptno = t.deptno)

where e.sal > t.asal


```


相关子查询写法


```

select * from emp e1

where e1.sal > (select AVG(sal) from emp

e2 where e2.deptno = e1.deptno)


```


![image](//upload-images.jianshu.io/upload_images/15606715-3e30a28848449114.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/581/format/webp)


**相关子查询的特点:外部查询执行一行数据,子查询执行一次**


![image](//upload-images.jianshu.io/upload_images/15606715-717174155c2451b4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)


##### 4.2 EXIST关键字


子查询用于验证外部查询的一行记录是否符合子查询的条件

示例9:查询是经理的员工的员工信息


```

select *

from emp e1

where exists (select empno from emp e2

where e2.mgr = e1.empno)


```


![image](//upload-images.jianshu.io/upload_images/15606715-c69f450311213bf4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/571/format/webp)


![image](//upload-images.jianshu.io/upload_images/15606715-e16fb232a57dbaa4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000/format/webp)


如果想表述不存在的逻辑,使用NOT EXISTS

C���P

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ### 1\. 笛卡尔积(交叉连接) 笛卡尔积是关系型数据库,进行多表查询的基础 A{a,b,c} B{d,e,f...
    lbon阅读 256评论 0 0
  • 一、使用子查询 语法格式 SELECT select_listFROM tableWHERE expr opera...
    辽A丶孙悟空阅读 2,906评论 0 43
  • 引出 •请思考如下问题? –查询所有员工的每个月工资总和,平均工资? –查询工资最高和最低的工资是多少? –查询公...
    C_cole阅读 7,321评论 0 3
  • 回到了赣州。南方的天气很热,热的使人烦躁不安,夜晚都难以入睡;东北的夏天也热,但是我能够睡得舒适、踏实…我突然想回...
    烨挽清风阅读 244评论 0 0
  • 今天对学生发火了,我很后悔。无论家教还是在上课,许多次都会发生这种情况:我讲了许多次的知识点,讲了多次的题,...
    nlx阅读 378评论 2 2