【Day08】

目录

MySQL-多表查询-概述

MySQL-多表查询-内连接

MySQL-多表查询-外连接

MySQL-多表查询-[标量、列]子查询

MySQL-多表查询-[行、表]子查询

MySQL-多表查询-案例

MySQL-事务-介绍与操作

MySQL-事务-四大特性

MySQL-索引-介绍

MySQL-索引-结构

MySQL-索引-操作语法

Mybatis-入门-课程介绍

Mybatis-入门-快速入门程序

Mybatis-入门-配置SQL提示

Mybatis-入门-JDBC介绍

Mybatis-入门-数据连接池

Mybatis-入门-lombok工具包


MySQL-多表查询-概述

sql 复制代码
-- 多表查询 --
select * from tb_emp, tb_dept where tb_emp.dept_id = tb_dept.id;

-- 起别名
select * from tb_emp e, tb_dept d where e.dept_id = d.id;

MySQL-多表查询-内连接

sql 复制代码
-- 内连接 --
-- A.查询员工的姓名 , 及所属的部门名称 (隐式内连接实现)
select tb_emp.name, tb_dept.name
from tb_emp,
     tb_dept
where tb_emp.dept_id = tb_dept.id;

-- 起别名
select e.name, d.name from tb_emp e, tb_dept d where e.dept_id = d.id;

-- B.查询员工姓名 , 及所属的部门名称 (显示内连接实现)
select tb_emp.name, tb_dept.name from tb_dept inner join tb_emp  on tb_emp.dept_id = tb_dept.id;

MySQL-多表查询-外连接

sql 复制代码
-- 外连接 --
-- A.查询员工表 所有 员工的姓名, 和对应部门名称 (左外连接) 会完全包含左表的数据
select tb_emp.name, tb_dept.name from tb_emp left outer join tb_dept on tb_emp.dept_id = tb_dept.id;


-- B.查询部门表 所有 部门的名称, 和对应的员工名称 (右外连接)会完全包含右表的数据
select tb_dept.name, tb_emp.name from tb_emp  right outer join tb_dept  on tb_emp.dept_id = tb_dept.id;

MySQL-多表查询-[标量、列]子查询

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

-- b.再查询该部门ID下的员工信息
select * from tb_emp where dept_id = 2;

-- 整合
select * from tb_emp where dept_id = (select id from tb_dept where name = '教研部');


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

-- b.查询再 '方东白' 入职之后的员工信息
select * from tb_emp where entrydate >= '2012-11-01';

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


-- 列子查询
-- A.查询 "教研部"和"咨询部"的所有员工信息
-- a.查询 "教研部"和"咨询部"部门ID - tb_dept
select id from tb_dept where name = '教研部' || name = '咨询部';

-- b.根据部门ID,查询该部门下的员工信息 - tb_emp
select * from tb_emp where dept_id in (2, 3);

-- 整合
select * from tb_emp where dept_id in (select id from tb_dept where name = '教研部' || name = '咨询部');

MySQL-多表查询-[行、表]子查询

sql 复制代码
-- 行子查询
-- A.查询与"韦一笑"的入职日期及职位都相同的员工信息
-- a.查询"韦一笑"的入职日期和职位
select entrydate, job from tb_emp where name = '韦一笑';

-- b.查询与"韦一笑"入职日期和职位都相同的员工信息
select * from tb_emp where entrydate = '2007-01-01' and job = 2;
select * from tb_emp where (entrydate, job) = ('2007-01-01', 2);

-- 整合
select *from tb_emp where entrydate = (select entrydate from tb_emp where name = '韦一笑') and job = (select job from tb_emp where name = '韦一笑');
select * from tb_emp where (entrydate, job) = (select entrydate, job from tb_emp where name = '韦一笑');


-- 表子查询
-- A.查询入职日期是"2006-01-01"之后的员工信息,及其部门名称
-- a.查询入职日期之后的员工信息
select * from tb_emp where entrydate > '2006-01-01';

-- b.查询这部分员工信息及其部门名称
select e.*, d.name from (select * from tb_emp where entrydate > '2006-01-01') e, tb_dept d where e.dept_id = d.id;

MySQL-多表查询-案例

sql 复制代码
-- 需求:
-- 1.查询价格低于10元的菜品的名称、价格 及其 菜品的分类名称
-- 表 : dish , category
-- SQL :
select d.name, d.price, c.name
from dish d,
     category c
where d.price < 10
  and d.category_id = c.id;

-- 2.查询所有价格在 10元(含)到50元(含)之间 且 状态为"启售"的菜品,展示出菜品的名称(即使菜品没有分类,也需要将菜品查询出来)
-- 表 : dish , category
-- SQL :
select d.name, d.price, c.name
from dish d
         left join category c on d.category_id = c.id
where d.status = 1
  and d.price between 10 and 50;

-- 3.查询每个分类下最贵的菜品,展示出分类的名称、最贵的菜品的价格
-- 表 : dish , category
-- SQL :
select c.id, max(d.price)
from dish d,
     category c
where d.category_id = c.id
group by c.name;

-- 4.查询各个分类下 菜品状态为"启售",并且 该分类下菜品总数量大于等于3的 分类名称
-- 表 : dish , category
-- SQL :
select c.name, count(*)
from dish d,
     category c
where d.category_id = c.id
  and d.status = 1
group by c.name
having count(*) >= 3;

-- 5.查询出 "商务套餐A"中包含了哪些菜品 (展示出套餐名称、价格, 包含的菜品名称、价格、份数)
-- 表 : dish , setmeal , setmeal_dish
-- SQL :
select s.name, s.price, d.price, d.name, sd.copies
from setmeal s,
     setmeal_dish sd,
     dish d
where sd.dish_id = d.id
  and sd.setmeal_id = s.id
  and s.name = '商务套餐A';

-- 6.查询出低于菜品平均价格的菜品信息(展示出菜品名称、菜品价格)
-- 表 : dish
-- SQL :
select d.name, d.price from dish d where d.price < (select avg(price) from dish);

MySQL-事务-介绍与操作

sql 复制代码
-- 开启事务
start transaction;

-- 删除部门
delete from tb_dept where id=3;

-- 删除部门下的员工
delete from tb_emp where dept_id = 3;

-- 提交事务
commit;

-- 回滚事务
rollback;

MySQL-事务-四大特性

MySQL-索引-介绍

MySQL-索引-结构

MySQL-索引-操作语法

sql 复制代码
-- ===================================== 索引 ======================================
-- 创建 : 为tb_emp表的name字段创建索引
create index idx_emp_name on tb_emp(name);

-- 查询 : 查询 tb_emp 表的索引信息
show index from tb_emp;

-- 删除 : 删除 tb_emp 表中的name字段的索引
drop index idx_emp_name on tb_emp;

Mybatis-入门-课程介绍

Mybatis-入门-快速入门程序

Mybatis-入门-配置SQL提示

Mybatis-入门-JDBC介绍

Mybatis-入门-数据连接池

Mybatis-入门-lombok工具包

相关推荐
qq_3482318543 分钟前
MySQL 与 PostgreSQL PL/pgSQL 的对比详解
数据库·mysql·postgresql
cui_win1 小时前
Prometheus实战教程 - mysql监控
mysql·prometheus·压测
wsx_iot1 小时前
mysql的快照读和当前读
数据库·mysql
梁萌1 小时前
MySQL分区表使用保姆级教程
数据库·mysql·优化·分区表·分区·partitions
期待のcode2 小时前
MyBatis-Plus的Wrapper核心体系
java·数据库·spring boot·后端·mybatis
Logic1012 小时前
《数据库运维》 郭文明 实验4 数据库备份与恢复实验核心操作与思路解析
运维·数据库·sql·mysql·学习笔记·形考作业·国家开放大学
hssfscv3 小时前
Mysql学习笔记——多表查询
笔记·学习·mysql
MC皮蛋侠客3 小时前
MySQL数据库迁移脚本及使用说明
数据库·mysql
soft20015253 小时前
《Rocky Linux 9.6 部署 MySQL 8.0 生产手册(含错误处理)》
linux·mysql·adb
帝吃藕和4 小时前
MySQL 知识点复习- 6. inner/right/left join
mysql