MySQL基础练习题29-员工奖金

目录

题目

准备数据

分析数据

总结


题目

报告每个奖金 少于 1000 的员工的姓名和奖金数额。

准备数据

## 创建库
create database db;
use db;

## 创建employee表
Create table If Not Exists Employee (empId int, name varchar(255), supervisor int, salary int);

## 创建bonus表
Create table If Not Exists Bonus (empId int, bonus int);

## 向表中插入数据
Truncate table Employee;
insert into Employee (empId, name, supervisor, salary) values ('3', 'Brad', null, '4000');
insert into Employee (empId, name, supervisor, salary) values ('1', 'John', '3', '1000');
insert into Employee (empId, name, supervisor, salary) values ('2', 'Dan', '3', '2000');
insert into Employee (empId, name, supervisor, salary) values ('4', 'Thomas', '3', '4000');
Truncate table Bonus;
insert into Bonus (empId, bonus) values ('2', '500');
insert into Bonus (empId, bonus) values ('4', '2000');

 employee表

bonus表

分析数据

第一步:第一步将两个表进行左连接,如果右表不存在左表,就会出现null

select * from employee e left join bonus b on b.empId = e.empId;

第二步:选出奖金<1000,而且还要考虑为null的情况

select name,bonus from employee e
left join bonus b on b.empId = e.empId
where bonus < 1000 or bonus is null;

 

总结

左外连接 根据条件查询,当右表数据不存在时,则会使用null值来填充。

select 字段名 from A表名 别名 left [outer] join B表名 别名 on 条件;

右外连接如果根据条件查询,当左表数据不存在时,则会使用null填充值。

select 字段名 from A表名 别名 right [outer] join B表名 别名 on 条件;

  • 14
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值