We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
题目链接:计算特殊奖金
编写解决方案,计算每个雇员的奖金。如果一个雇员的 id 是 奇数 并且他的名字不是以 'M' 开头,那么他的奖金是他工资的 100% ,否则奖金为 0 。
返回的结果按照 employee_id 排序。
Create table If Not Exists Employees (employee_id int, name varchar(30), salary int); Truncate table Employees; insert into Employees (employee_id, name, salary) values ('2', 'Meir', '3000'); insert into Employees (employee_id, name, salary) values ('3', 'Michael', '3800'); insert into Employees (employee_id, name, salary) values ('7', 'Addilyn', '7400'); insert into Employees (employee_id, name, salary) values ('8', 'Juan', '6100'); insert into Employees (employee_id, name, salary) values ('9', 'Kannon', '7700');
本题考察了三个知识点:
name
M
LIKE
REGEXP
M%
^M
^M.*
^[^M]
RLIKE
6
bonus
0
IF
IF (condition, true, false)
CASE
CASE WHEN condition THEN true ELSE false END
掌握了上面的方法,你就可以写出 24 种 SQL 语句了,下面是其中一种
24
SQL
SELECT employee_id, IF (employee_id % 2 != 0 AND name NOT LIKE 'M%', salary, 0) bonus FROM Employees ORDER BY employee_id;
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目
题目链接:计算特殊奖金
编写解决方案,计算每个雇员的奖金。如果一个雇员的 id 是 奇数 并且他的名字不是以 'M' 开头,那么他的奖金是他工资的 100% ,否则奖金为 0 。
返回的结果按照 employee_id 排序。
解析
本题考察了三个知识点:
name
中首字母是M
的方法有LIKE
和REGEXP
两种:LIKE
,用左匹配:M%
REGEXP
,正则匹配有很多种,正则的写法有很多种,就不一一列举了^M
:以M
开头^M.*
:以M
开头,后面跟任意字符^[^M]
:以非M
开头REGEXP
类似的RLIKE
,也是匹配正则6
种方法判断奇数bonus
否者输出0
,有两种方法:IF
:IF (condition, true, false)
CASE
:CASE WHEN condition THEN true ELSE false END
掌握了上面的方法,你就可以写出
24
种SQL
语句了,下面是其中一种SQL
The text was updated successfully, but these errors were encountered: