Mysql:临时表方法,实现多表查询第二高工资

salaries表:

employees表: 

departments表:

查询sql语句:

 

use employee;
DROP TABLE IF EXISTS tTable;
DROP TABLE IF EXISTS max_salary;
DROP TABLE IF EXISTS exclude_max_salary;
CREATE TEMPORARY TABLE tTable AS
(
    select *  from salaries
    natural join employees
    natural join departments
    where departments.dept_name = "Risk Management"
);

create temporary table max_salary as (
select max(salary) as salary from tTable
);

create temporary table exclude_max_salary as (
	select max(salary) as salary from tTable
		where salary != (
			select salary from max_salary
		)
);

select * from tTable
where salary = (
	select salary from exclude_max_salary
);