Score
0
Watch 44 Star 256 Fork 69

xuthus / 数据库SQL实战SQLMIT

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
59.获取有奖金的员工相关信息.md 1.93 KB
Copy Edit Web IDE Raw Blame History
xuthus authored 2019-08-12 23:23 . add:all

获取有奖金的员工相关信息

题目描述

获取有奖金的员工相关信息。

给出emp_no、first_name、last_name、奖金类型btype、对应的当前薪水情况salary以及奖金金额bonus。 bonus类型btype为1其奖金为薪水salary的10%,btype为2其奖金为薪水的20%,其他类型均为薪水的30%。 当前薪水表示to_date='9999-01-01'

CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
create table emp_bonus(
emp_no int not null,
recevied datetime not null,
btype smallint not null);
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL, PRIMARY KEY (`emp_no`,`from_date`));

答案

-- 一般答案
select eb.emp_no,e.first_name,e.last_name,eb.btype,s.salary,(s.salary*btype*0.1) bonus from emp_bonus eb,employees e,salaries s where eb.emp_no = e.emp_no and eb.emp_no = s.emp_no and s.to_date = '9999-01-01'

-- 利用条件判断解法
select eb.emp_no,
       e.first_name,
       e.last_name,
       eb.btype,
       s.salary,
       (case btype
           when 1 then s.salary*0.1
           when 2 then s.salary*0.2
           when 3 then s.salary*0.3
           end
           ) bonus
from emp_bonus eb,
     employees e,
     salaries s
where eb.emp_no = e.emp_no
  and eb.emp_no = s.emp_no
  and s.to_date = '9999-01-01'

题解

简单的拼表筛选可以解决问题。但是考点是case-when语句

语法规则:

case colume 
    when condition then result
    when condition then result
    when condition then result
    else result
end

Comment ( 0 )

Sign in for post a comment