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 = '咨询部');
相关推荐
洛神灬殇15 分钟前
【LLM大模型技术专题】「入门到精通系列教程」基于ai-openai-spring-boot-starter集成开发实战指南
网络·数据库·微服务·云原生·架构
小鸡脚来咯25 分钟前
redis分片集群架构
数据库·redis·架构
全都是浮夸丶1 小时前
MVCC理解
mysql
christine-rr1 小时前
征文投稿:如何写一份实用的技术文档?——以软件配置为例
运维·前端·网络·数据库·软件构建
HyggeBest1 小时前
Mysql 宏观架构
mysql·架构
海尔辛1 小时前
SQL 基础入门
数据库·sql
jiunian_cn2 小时前
【Linux】Linux权限
linux·服务器·mysql
betazhou3 小时前
有没有 MariaDB 5.5.56 对应 MySQL CONNECTION_CONTROL 插件
linux·数据库·mysql·oracle·mariadb
Elohim8153 小时前
数据库SQLite基础
数据库·sqlite
TDengine (老段)3 小时前
TDengine 支持的平台汇总
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据