MySQL中不允许使用列别名作为查询条件

在MySQL中有个特殊的规定,即不允许使用列别名作为查询条件。比如有下面一个表:

select 
    ID, 
    title, 
    concept, 
    conceptLength, 
    addUserId, 
    modifyTime
from collections_wisdom

将SQL修改如下:

select 
    ID+1 as newID
    title, 
    concept, 
    conceptLength, 
    addUserId, 
    modifyTime
from collections_wisdom
where newID>2

那么,执行起来就会出现异常:StatementCallback; bad SQL grammar

实在要执行,只好把新字段的组成在条件里再实现一遍,如下:

select 
    ID+1 as newID, 
    title, 
    concept, 
    conceptLength, 
    addUserId, 
    modifyTime
from collections_wisdom
where (ID+1)>2

之所以MySQL中不允许使用列别名作为查询条件,据说是因为MySql中列的别名本来是返回结果的时候才显示的,不在SQL解析时候使用。在没有更令人信服的解释出现前,权且当做这样吧。

原文地址:https://www.cnblogs.com/doudouxiaoye/p/5773749.html