Suppose we have some employees in each department.we have total 3 departments . Below is the sample source table named 'employee'
emp dept_id salary
A 10 1000
B 10 2000
C 10 3000
D 20 7000
E 20 9000
F 20 8000
G 30 17000
H 30 15000
I 30 30000
j 30 30000
k 30 17000
Here may same salary is exist in same department.
I use Wamp-server which has mysql-5.7.23
And I want to like:
B 10 2000
F 20 8000
G 30 17000
I think there are several way to solve the problem. Following solution from my side and works fine.
SELECT *
From employee e2
WHERE e2.salary = (SELECT distinct salary FROM employee where dept_id=e2.dept_id order by salary desc limit 1,1);
I need only second highest salary value with department wise which is the input array of next operation in my project. Finally I use
SELECT e2.dept_id, max(e2.salary) From employee e2 WHERE e2.salary = (SELECT distinct salary FROM employee where dept_id=e2.dept_id order by salary desc limit 1,1) group by e2.dept_id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.