在下面的代码中,如果我用where子句替换having子句,我会在where子句中得到1054错误说明未知列tsal。为什么?还请特别指出我必须使用的情况以及我必须使用where子句的地方。
select employee_id,salary*months as tsal1
from Employee as h
having tsal1=(select max(e.tsal) as metsal
from (select employee_id,salary*months as tsal
from Employee) as e)
答案 0 :(得分:0)
MySql确实not allow您要在select
子句中引用别名(在where
子句中定义)。
要解决此问题,只需重复该别名的表达式:
select employee_id, salary*months as tsal1
from Employee as h
where salary*months=(select max(e.tsal) as metsal
from (select employee_id,salary*months as tsal
from Employee) as e)
注意:不需要有两个内部子查询。您可以简化为:
select employee_id, salary*months as tsal1
from Employee as h
where salary*months=(select max(salary*months) from Employee)