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

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