三种数据库的执行顺序,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>

mysql查询的执行顺序是:
1、FROM table1 left join table2 on 将table1和table2中的数据产生笛卡尔积,生成Temp1
2、JOIN table2 所以先是确定表,再确定关联条件
3、ON table1.column = table2.columu 确定表的绑定条件 由Temp1产生中间表Temp2
4、WHERE 对中间表Temp2产生的结果进行过滤 产生中间表Temp3
5、GROUP BY 对中间表Temp3进行分组,产生中间表Temp4
6、HAVING 对分组后的记录进行聚合 产生中间表Temp5
7、SELECT 对中间表Temp5进行列筛选,产生中间表 Temp6
8、DISTINCT 对中间表 Temp6进行去重,产生中间表 Temp7
9、ORDER BY 对Temp7中的数据进行排序,产生中间表Temp8
10、LIMIT 对中间表Temp8进行分页,产生中间表Temp9

所以在where执行的时候,别名还不存在,而order by的时候已经存在

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

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值