从第二个查询的平均值中减去第一个查询的平均值

时间:2018-10-07 11:56:54

标签: mysql sql join subquery

我想找到飞行员的平均工资与包括飞行员在内的员工的平均工资之间的差,所以基本上是(平均值(飞行员)-平均值(雇员))。

尽管我很难将两个SQL查询结合在一起,但到目前为止,这是我已经设法完成的事情:

SELECT AVG(salary) AS 'average(pilot)'
  FROM employees e, certified c
 WHERE e.EID  IN (SELECT EID FROM certified GROUP BY eid);

SELECT AVG(salary) AS 'average(employees)'
  FROM employees e, certified c
 WHERE e.EID NOT IN (SELECT EID FROM certified GROUP BY eid);

现在,我只需要从第一个查询中减去第二个查询的结果。我该如何进行?

1 个答案:

答案 0 :(得分:0)

select (select query2) - (select query1);

我想这应该可行,

select (select avg(salary) as 'average(employees)'
from employees e, certified c
where e.EID not in (select EID from certified group by eid)) - (select  avg(salary) as 'average(pilot)'
from employees e, certified c
where e.EID  in (select EID from certified group by eid));

请参考this,以获得类似的答案