绵阳做手机网站建设,wordpress 自适应 汉化,邙山网站建设,wordpress阿里巴巴图标大家好#xff0c;我是空空star#xff0c;本篇带大家了解一道简单的力扣sql练习题。 文章目录前言一、题目#xff1a;1873. 计算特殊奖金二、解题1.正确示范①提交SQL运行结果2.正确示范②提交SQL运行结果3.正确示范③提交SQL运行结果4.正确示范④提交SQL运行结果5.其他总… 大家好我是空空star本篇带大家了解一道简单的力扣sql练习题。 文章目录前言一、题目1873. 计算特殊奖金二、解题1.正确示范①提交SQL运行结果2.正确示范②提交SQL运行结果3.正确示范③提交SQL运行结果4.正确示范④提交SQL运行结果5.其他总结前言 一、题目1873. 计算特殊奖金
表: Employees
----------------------
| 列名 | 类型 |
----------------------
| employee_id | int |
| name | varchar |
| salary | int |
----------------------
employee_id 是这个表的主键。
此表的每一行给出了雇员id 名字和薪水。 写出一个SQL 查询语句计算每个雇员的奖金。如果一个雇员的id是奇数并且他的名字不是以’M’开头那么他的奖金是他工资的100%否则奖金为0。 Return the result table ordered by employee_id. 返回的结果集请按照employee_id排序。 查询结果格式如下面的例子所示。 输入
Employees 表:
------------------------------
| employee_id | name | salary |
------------------------------
| 2 | Meir | 3000 |
| 3 | Michael | 3800 |
| 7 | Addilyn | 7400 |
| 8 | Juan | 6100 |
| 9 | Kannon | 7700 |
------------------------------
输出
--------------------
| employee_id | bonus |
--------------------
| 2 | 0 |
| 3 | 0 |
| 7 | 7400 |
| 8 | 0 |
| 9 | 7700 |
-------------------- 解释 因为雇员id是偶数所以雇员id 是2和8的两个雇员得到的奖金是0。 雇员id为3的因为他的名字以’M’开头所以奖金是0。 其他的雇员得到了百分之百的奖金。 二、解题
1.正确示范①
提交SQL
select employee_id,
case when employee_id%21 and name not like M% then salary
else 0 end bonus
from Employees
order by employee_id;运行结果 2.正确示范②
提交SQL
select employee_id,
case when mod(employee_id,2)1 and left(name,1)!M then salary
else 0 end bonus
from Employees
order by employee_id;运行结果 3.正确示范③
提交SQL
select employee_id,
if(mod(employee_id,2)1 and left(name,1)!M,salary,0) bonus
from Employees
order by employee_id;运行结果 4.正确示范④
提交SQL
select employee_id,
case when mod(employee_id,2)0 then 0
when mod(employee_id,2)1 and left(name,1)M then 0
else salary end bonus
from Employees
order by employee_id;运行结果 5.其他 总结 正确示范①思路 如果一个雇员的id是奇数并且他的名字不是以’M’开头那么他的奖金是他工资的100%也就是他的工资否则奖金为0。 采用 case when %取余 not like case when employee_id%21 and name not like M% then salary else 0 end 正确示范②思路 思路同① 采用 case when mod取余 left case when mod(employee_id,2)1 and left(name,1)!M then salary else 0 end 正确示范③思路 思路同① 采用 if mod取余 left if(mod(employee_id,2)1 and left(name,1)!M,salary,0) 正确示范④思路 雇员id是偶数则奖金为0 雇员id是奇数并且他的名字以’M’开头则奖金为0 否则奖金是他工资的100% case when mod(employee_id,2)0 then 0 when mod(employee_id,2)1 and left(name,1)M then 0 else salary end