#子查询-某些情况下,当进行查询的时候,需要的条件是另外一个select语句的结果,这个时候就要用到子查询。用于子查询的关键字主要包括:
in、not in、=、!=、exists、not exists等等。
#从emp表中查询出所有部门在dept表中的所有记录 mysql> select * from dept;
+--------+----------+
| deptno | deptname |
+--------+----------+
| 1 | tech |
| 2 | sale |
| 3 | hr |
| 4 | sl |
+--------+----------+
4 rows in set (0.01 sec) mysql> select * from emp;
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 |
| zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 |
| ttx2 | 2023-04-10 | 2010-03-04 | 4000.00 | 4 | 23 |
| ssss | 2019-01-01 | 2018-01-01 | 5000.00 | 2 | 24 |
+-------+------------+------------+---------+--------+------+
4 rows in set (0.00 sec) mysql> select * from emp where deptno in(select deptno from dept);
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 |
| ssss | 2019-01-01 | 2018-01-01 | 5000.00 | 2 | 24 |
| zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 |
| ttx2 | 2023-04-10 | 2010-03-04 | 4000.00 | 4 | 23 |
+-------+------------+------------+---------+--------+------+
4 rows in set (0.00 sec) #如果子查询记录数唯一,还可以用=代替in mysql> select * from emp where deptno=(select deptno from dept limit 1); +-------+------------+------------+---------+--------+------+ | ename | birth | hirdate | sal | deptno | age1 | +-------+------------+------------+---------+--------+------+ | zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 | +-------+------------+------------+---------+--------+------+ 1 row in set (0.00 sec) # 某些情况下,子查询可转化为表连接 mysql> select * from emp where deptno in(select deptno from dept); +-------+------------+------------+---------+--------+------+ | ename | birth | hirdate | sal | deptno | age1 | +-------+------------+------------+---------+--------+------+ | zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 | | ssss | 2019-01-01 | 2018-01-01 | 5000.00 | 2 | 24 | | zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 | | ttx2 | 2023-04-10 | 2010-03-04 | 4000.00 | 4 | 23 | +-------+------------+------------+---------+--------+------+ 4 rows in set (0.00 sec) #转换为表连接后
mysql> select emp.* from emp,dept where emp.deptno=dept.deptno;
+-------+------------+------------+---------+--------+------+
| ename | birth | hirdate | sal | deptno | age1 |
+-------+------------+------------+---------+--------+------+
| zzx1 | 2000-01-01 | 2000-01-01 | 2000.00 | 1 | 21 |
| ssss | 2019-01-01 | 2018-01-01 | 5000.00 | 2 | 24 |
| zzx1 | 2002-03-09 | 2009-04-03 | 2001.00 | 3 | 22 |
| ttx2 | 2023-04-10 | 2010-03-04 | 4000.00 | 4 | 23 |
+-------+------------+------------+---------+--------+------+
4 rows in set (0.00 sec) #union和union all的主要区别是union all是把结果集直接合并在一起
union是将union all后的结果在进行一次distinct,去除重复记录后的结果。
mysql> select deptno from emp union all select deptno from dept;
+--------+
| deptno |
+--------+
| 1 |
| 3 |
| 4 |
| 2 |
| 1 |
| 2 |
| 3 |
| 4 |
+--------+
8 rows in set (0.00 sec) mysql> select deptno from emp union select deptno from dept;
+--------+
| deptno |
+--------+
| 1 |
| 3 |
| 4 |
| 2 |
+--------+
4 rows in set (0.00 sec)

最新文章

  1. Aircrack-ng: (2) WEP & WPA/WPA2 破解
  2. 解决mac安装grunt时出现[command not found]的错误
  3. Python多行注释
  4. Linux网络编程&内核学习
  5. CMD窗口正确显示UTF-8字符
  6. iOS开发中.pch 文件的使用及其相关工程设置
  7. java笔记7之录入
  8. python连接redis002
  9. Android_BitmapShader实现圆形、圆角图片
  10. Demo 示例控制输入光标位置
  11. Sql Server 存储过程中查询数据无法使用 Union(All)
  12. cmder修改默认打开路径
  13. Spring详解(七)------事务管理
  14. FastDFS 简介
  15. 关于node的前端项目编译时内存溢出问题
  16. DAY4-打卡第四天-2018-1-12
  17. 第五章 JavaScript对象及初识面向对象
  18. linux设置oracle自动启动
  19. Python小练习之判断一个日期是一年的第几天
  20. nova 命令管理虚拟机

热门文章

  1. 爬取房价信息并制作成柱状图XPath,pyecharts
  2. Pycharm关联gitlab(http方式)
  3. (6java)计算机语言发展史
  4. Go是一门什么样的语言?
  5. ts踩坑笔记
  6. memcache(11211)未授权访问
  7. GIT·全局配置文件及项目配置文件
  8. 探讨UE4中的UBT和UHT
  9. dubbo学习实践(3)之Dubbo整合Consul及Dubbo配置方式
  10. 安全工具推荐之sqlmap tamper&sqlmap api