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 = '咨询部');
相关推荐
Databend11 小时前
在 AWS 中国峰会逛了一天,我在 Databend 展台看到了 Agent 数据基础设施的新思路
数据库·人工智能·agent
李白客2 天前
KES新版MySQL兼容能力再升级意味着什么?
mysql·国产数据库
ClouGence2 天前
Oracle 数据同步为什么会出现数据不一致?长事务是常被忽略的原因
数据库·后端·oracle
飞将2 天前
从零实现数据库(2)——HashIndex + IndexManager
数据库
Nturmoils3 天前
订单列表慢查询,先看 WHERE、ORDER BY 和 LIMIT
数据库
渣波3 天前
拒绝 SQL 焦虑!手把手带你用 NestJS + Prisma + DTO 写出“防弹”级后端代码
javascript·数据库·后端
Jim6003 天前
【吃透 MySQL InnoDB连载】第 1 章・解密线上数据库高频故障
mysql
GreatSQL4 天前
gt-checksum v4.0.0 新功能解读系列文章(4):SSL 加密连接——数据校验传输安全再升级
mysql
倔强的石头_4 天前
KingbaseES 新版MySQL 兼容版体验:旧版迁移 + 功能实测
数据库