SQL語句Where中不能使用別名作爲判斷條件

今天mysql發現sql語句where條件判斷時不能使用別名作爲判斷條件

SELECT
            t3.cellphone,
            t3.account_state,
            t3.address,
            t3.area,
            t3.city,
            t3.company,
            t3.customer_code,
            t3.province,
            t3.username,
            t3.wx_user,
            t3.wx_user_pic,
            t2.platform_name,
            t1.id,
            t1.account_id,
            t1.platform_id,
            t1.account_state as platformAccountState,
            t1.account_type,
            t1.account_grade,
            t1.account_grade_code
        FROM
            account_platform_relation t1
        LEFT JOIN operation_platform t2 ON t1.platform_id = t2.id
        LEFT JOIN account_audited t3 ON t1.account_id = t3.account_id
WHERE t2.platform_state = '1'
AND platformAccountState = '1'====>此處

執行報錯—未知列

[SQL]SELECT
            t3.cellphone,
            t3.account_state,
            t3.address,
            t3.area,
            t3.city,
            t3.company,
            t3.customer_code,
            t3.province,
            t3.username,
            t3.wx_user,
            t3.wx_user_pic,
            t2.platform_name,
            t1.id,
            t1.account_id,
            t1.platform_id,
            t1.account_state as platformAccountState,
            t1.account_type,
            t1.account_grade,
            t1.account_grade_code
        FROM
            account_platform_relation t1
        LEFT JOIN operation_platform t2 ON t1.platform_id = t2.id
        LEFT JOIN account_audited t3 ON t1.account_id = t3.account_id
WHERE t2.platform_state = '1'
AND platformAccountState = '1'
[Err] 1054 - Unknown column 'platformAccountState' in 'where clause'

oracle的執行順序
from–where–group by–having–select–order by,
from:需要從哪個數據表檢索數據
where:過濾表中數據的條件
group by:如何將上面過濾出的數據分組
having:對上面已經分組的數據進行過濾的條件
select:查看結果集中的哪個列,或列的計算結果
order by :按照什麼樣的順序來查看返回的數據

sqlserver查詢的執行順序是:
(1)FROM <left_table> <join_type> JOIN <right_table> ON <on_predicate>
(2)WHERE <where_predicate>
(3)GROUP BY <group_by_specification>
(4)HAVING <having_predicate>
(5)SELECT DISTINCT TOP(<top_specification>) <select_list>
(6)ORDER BY <order_by_list>

所以在where執行的時候,別名還不存在,而order by的時候已經存在

如果非要用別名,那麼只能用派生表,即先生成別名再where
select * from
(
select 字段1 as A,字段2 as B… from tablename
) aaa
where A=1
order by A

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章