簡體   English   中英

為什么where子句在我的SQL查詢中不起作用?

[英]Why does where clause not work in my sql query?

在下面的代碼中,如果我將where子句替換為where子句,則會收到1054錯誤,其中說where子句中的列tsal未知。 為什么? 也請給我一些啟發,特別是在必須使用had以及必須使用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)

MySql 不允許您從where子句中引用別名(在select子句中定義)。

要解決此問題,只需對該別名重復表達式:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM