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 = '咨询部');
相关推荐
XDHCOM8 小时前
ORA-32484重复列名错误,ORACLE数据库CYCLE子句故障修复与远程处理方案
数据库·oracle
翻斗包菜8 小时前
PostgreSQL 日常维护完全指南:从基础操作到高级运维
运维·数据库·postgresql
呆瑜nuage8 小时前
MySQL表约束详解:8大核心约束实战指南
数据库·mysql
liliangcsdn9 小时前
Agent Memory智能体记忆系统的示例分析
数据库·人工智能·全文检索
那个失眠的夜9 小时前
Mybatis延迟加载策略
xml·java·数据库·maven·mybatis
Rick19939 小时前
SQL 执行流程
数据库·sql
M--Y9 小时前
Redis常用数据类型
数据结构·数据库·redis
元宝骑士9 小时前
FIND_IN_SET使用指南:场景、优缺点与MySQL优化策略
后端·mysql
猿小喵9 小时前
MySQL慢查询分析与处理-第二篇
数据库·mysql·性能优化
Y0011123610 小时前
MySQL-进阶
开发语言·数据库·sql·mysql