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 = '咨询部');
相关推荐
AAA修煤气灶刘哥11 小时前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
程序新视界12 小时前
学习MySQL绕不开的两个基础概念:聚集索引与非聚集索引
mysql
RestCloud15 小时前
跨境数据传输:ETL如何处理时区与日期格式差异
mysql·api
RestCloud15 小时前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术18 小时前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
xiaok19 小时前
mysql中怎么创建一个可控权限数据库账号密码给到开发者
mysql
可涵不会debug1 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom1 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
玉衡子1 天前
九、MySQL配置参数优化总结
java·mysql
麦兜*1 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud