MySQL-25.多表查询-子查询(标量、列)

一.子查询

二.标量子查询

sql 复制代码
-- ============================================= 子查询 ==============================================
-- 标量子查询
-- A. 查询"教研部"的所有员工信息
-- a. 查询教研部的部门id    tb_dept
select id from tb_dept where name = '教研部';

-- b. 查询部门id为2的所有员工信息   tb_emp
select * from tb_emp where dept_id = 2;

-- 将两条子查询合并
select * from tb_emp where dept_id = (select id from tb_dept where name = '教研部');

-- B. 查询在"方东白"入职之后的员工信息
-- a.查询"方东白"的入职时间   tb-emp
select entrydate from tb_emp where name = '方东白';

-- b.查询entrydate大于方东百的员工信息  tb-emp
select * from tb_emp where entrydate > '2012-11-01';

select * from tb_emp where entrydate > (select entrydate from tb_emp where name = '方东白');

三.列子查询

sql 复制代码
-- 列子查询
-- A.查询"教研部"和"咨询部"的所有员工信息
-- a.查询"教研部"和"咨询部"的部门id
select id from tb_dept where name = '教研部' or name = '咨询部';

-- b.查询员工部门id等于2或者3的员工信息
select * from tb_emp where dept_id in (3,2);

select * from tb_emp where dept_id in (select id from tb_dept where name = '教研部' or name = '咨询部');
相关推荐
BD_Marathon42 分钟前
【MySQL】函数
android·数据库·mysql
她说..2 小时前
Redis项目应用总结(苍穹外卖/黑马头条/乐尚代驾)
java·数据库·redis·缓存·消息队列·redisson·geo
蒋士峰DBA修行之路2 小时前
实验二十 GaussDB逻辑备份恢复实验
数据库·gaussdb
gsfl3 小时前
Redis 常见面试题
数据库·redis·缓存
musenh3 小时前
mysql多表查询
mysql
Morpheon4 小时前
A Guide to Data System Storage: From Basics to Advanced File Structures
数据库
Penge6664 小时前
MySQL-隐式类型转换的 “隐形陷阱”
后端·mysql
yzx9910135 小时前
Django 配置与安装完整指南
数据库·django·sqlite
東雪蓮☆5 小时前
LNMP 环境部署 WordPress
linux·运维·mysql·nginx·php
Archie_IT7 小时前
「深入浅出」嵌入式八股文—P2 内存篇
c语言·开发语言·数据结构·数据库·c++·算法