隐式内链接
select 字段列表 from 表1,表2 where 条件...
练习
-- 内连接
# 1.查询员工的姓名,及所属的部门名称(隐式内连接实现)
select tb_emp.name,tb_dept.name from tb_emp,tb_dept where tb_emp.dept_id = tb_dept.id;
显式内连接
select 字段列表 from 表1 [inner] join 表2 on 连接条件...
练习
# 2.查询员工姓名,及所属的部门名称(显示内连接实现)
select e.name,d.name from tb_emp e inner join tb_dept d on e.dept_id = d.id;
左外连接
select 字段列表 from 表1 left [outer] join 表2 on 连接条件...
练习
#1.查询员工表所有员工的姓名和对应的部门名称(左连接)
select e.name,d.name from tb_emp e left join tb_dept d on e.dept_id = d.id;
右外连接
select 字段列表 from 表1 right [outer] join 表2 on 连接条件...
练习
#2.查询部门表所有部门的名称和对应的员工名称(右连接)
select e.name,d.name from tb_emp e right join tb_dept d on e.dept_id = d.id;
概述
分类
-- 标量子查询
#1.查询“教研部”的所有员工信息
select * from tb_emp where dept_id = (select id from tb_dept where name = '教研部');
#2.查询在“房东白”入职之后的员工信息
select * from tb_emp where entrydate > (select a.entrydate from tb_emp a where name = '方东白');
-- 列子查询
# 查询“教研部”和“咨询部”所有的员工信息
select * from tb_emp where dept_id in (select id from tb_dept where name in ('教研部','咨询部'));
-- 行子查询
# 查询与 “韦一笑”的入职日期及职位都相同的员工信息
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 = '韦一笑');
# 查询入职日期是'2006-01-01'之后的员工信息,及其部门名称
select e.*,d.name from (select * from tb_emp where entrydate > '2006-01-01')
e join tb_dept d on e.dept_id = d.id;
-- 1. 查询价格低于 10元 的菜品的名称 、价格 及其 菜品的分类名称 .
select d.name, d.price, c.name
from dish d,
category c
where d.category_id = c.id
and d.price < 10;
-- 2. 查询所有价格在 10元(含)到50元(含)之间 且 状态为'起售'的菜品名称、价格 及其 菜品的分类名称 (即使菜品没有分类 , 也需要将菜品查询出来).
select d.name, d.price, c.name
from dish d
left join category c on d.category_id = c.id
where d.price between 10 and 50
and d.status = 1;
-- 3. 查询每个分类下最贵的菜品, 展示出分类的名称、最贵的菜品的价格 .
select max(d.price),c.name from dish d join category c on d.category_id = c.id group by c.id;
-- 4. 查询各个分类下 状态为 '起售' , 并且 该分类下菜品总数量大于等于3 的 分类名称 .
select c.name,count(*) from dish d,category c where d.category_id = c.id and d.status = 1 group by c.id having count(*)>=3;
-- 5. 查询出 "商务套餐A" 中包含了哪些菜品 (展示出套餐名称、价格, 包含的菜品名称、价格、份数).
select s.name,s.price,d.name,d.price,sd.copies from setmeal s right join setmeal_dish sd
on s.id = sd.setmeal_id
join dish d on d.id = sd.dish_id
where s.name = '商务套餐A';
select s.name,s.price,d.name,d.price,sd.copies from setmeal s,setmeal_dish sd,dish d where s.id = sd.setmeal_id and d.id = sd.dish_id and s.name='商务套餐A'
-- 6. 查询出低于菜品平均价格的菜品信息 (展示出菜品名称、菜品价格).
-- 总价格 select sum(price) from dish
-- 总数 select count(*) from dish
select * from dish where price < (select avg(price) from dish);
场景
操作
delete from tb_dept where id = 1;
delete from tb_emp where dept_id = 1;
问题
注意事项:默认MySQL的事务是自动提交的,也就是说,当执行一条DML语句,MySQL会立即隐式的提交事务
事务控制
start transaction / begin;
commit;
rollback;
MySQL数据库支持的索引结构有很多,例如:Hash索引、B+Tree索引、Full-Text索引等。我们平常所说的索引,如果没有特别指定,都是默认为B+Tree结构组织的索引
create [unique] index 索引名 on 表名(字段名);
show index from 表名;
drop index 索引名 on 表名;
使用Mybatis查询所有用户数据
步骤
编写SQL语句(注解/XML)
@Mapper //在运行时,会自动生成该接口的实现类对象,并且将该对象交给SpringIOC管理
public interface UserDao {
@Select("select * from user")
List<User> getUsers();
}
单元测试
@SpringBootTest
class SpringbootProject01ApplicationTests {
@Autowired
private UserDao userDao;
@Test
public void test(){
System.out.println(userDao.getUsers().toString());
}
}
配置SQL提示
【可能出现】:
@Test
public void testJdbc() throws Exception {
//1. 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2. 获取连接对象
String url = "jdbc:mysql://localhost:3306/mybatis";
String username = "root";
String password = "1234";
Connection connection = DriverManager.getConnection(url, username, password);
//3. 获取执行SQL的对象Statement,执行SQL,返回结果
String sql = "select * from user";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
//4. 封装结果数据
List<User> userList = new ArrayList<>();
while (resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
short age = resultSet.getShort("age");
short gender = resultSet.getShort("gender");
String phone = resultSet.getString("phone");
User user = new User(id,name,age,gender,phone);
userList.add(user);
}
//5. 释放资源
statement.close();
connection.close();
}
优势
标准接口:DataSource
【注意事项】:Lombok会在编译时,自动生成对应的java代码,我们使用lombok时,还需要安装lombok的插件(一般版本靠前的idea自带)