标签:not 多次 employee sql order 图片 row table 情况
一、
查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_noy以及其对应的薪水涨幅growth,并按照growth进行升序
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 `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`));
employees 中tem_no和salaries中tem_no插入,然后找最大最小,求值
1、
select sCurrent.emp_no,(sCurrent.salary-sStart.salary) as growth from (select e.emp_no,s.salary from employees as e,salaries as s where e.emp_no=s.emp_no and s.to_date="9999-01-01") as sCurrent --找到当前薪水 join (select e.emp_no,s.salary from employees as e,salaries as s where e.emp_no=s.emp_no and s.from_date=e.hire_date) as sStart --找到初始入职薪水 on sCurrent.emp_no=sStart.emp_no --合并表,相减 order by growth
生成新表,两个新表连接后再减,而不能在一个表里多次跨行减。
2、
更好理解
select a.emp_no,(b.salary-c.salary) as growth from employees as a --往该表添加 join salaries as b on a.emp_no=b.emp_no and b.to_date="9999-01-01" --加入当前薪水 join salaries as c on a.emp_no=c.emp_no and c.from_date=a.hire_date --加入最初薪水 order by growth
二、
标签:not 多次 employee sql order 图片 row table 情况
原文地址:https://www.cnblogs.com/ruo-li-suo-yi/p/8977088.html