题解 | #获取每个部门中当前员工薪水最高的相关信息#
获取每个部门中当前员工薪水最高的相关信息
http://www.nowcoder.com/practice/4a052e3e1df5435880d4353eb18a91c6
菜鸟题解,牛客网这道题不如力扣测试用例来的严格,而且也不能贡献测试用例,具体解答思路应该参考力扣的数据库184题-部门工资最高的员工.
我对于联结学艺不精,只会用一点点,欢迎指正,还在学习中.
select a.dept_no, a.emp_no,b.salary as max_salary from dept_emp a left join salaries b on a.emp_no = b.emp_no where (a.dept_no,b.salary) in ( select a.dept_no,max(b.salary) from dept_emp a left join salaries b on a.emp_no = b.emp_no group by dept_no ) order by dept_no
这个解题思路实际上是判定目前联结好的表内,是否有人的部门信息和薪资信息与每个部门最高薪水值一致,如果一致,就输出这个人的信息,该判定就可以避免了重复员工信息不被输出的问题.
排序可加可不加,主要看题目.
在此提供一份我的测试用例,可以用来自测用.:
drop table if exists `dept_emp` ; drop table if exists `salaries` ; 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 `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`)); INSERT INTO dept_emp VALUES(10001,'d001','1986-06-26','9999-01-01'); INSERT INTO dept_emp VALUES(10002,'d001','1996-08-03','9999-01-01'); INSERT INTO dept_emp VALUES(10003,'d001','1996-08-03','9999-01-01'); INSERT INTO dept_emp VALUES(10004,'d003','1986-06-26','9999-01-01'); INSERT INTO dept_emp VALUES(10005,'d004','1996-08-03','9999-01-01'); INSERT INTO dept_emp VALUES(10006,'d002','1996-08-03','9999-01-01'); INSERT INTO dept_emp VALUES(10007,'d002','1996-08-03','9999-01-01'); INSERT INTO salaries VALUES(10001,88958,'2002-06-22','9999-01-01'); INSERT INTO salaries VALUES(10002,72527,'2001-08-02','9999-01-01'); INSERT INTO salaries VALUES(10003,88958,'2001-08-02','9999-01-01'); INSERT INTO salaries VALUES(10004,8858,'2002-06-22','9999-01-01'); INSERT INTO salaries VALUES(10005,72527,'2001-08-02','9999-01-01'); INSERT INTO salaries VALUES(10006,8858,'2001-08-02','9999-01-01'); INSERT INTO salaries VALUES(10007,8858,'2001-08-02','9999-01-01');
虽然思路第一次的时候我并没有想出来,但是做过的题目,不看代码我也成功地能够重新做出来了,很开心~