【发布时间】:2012-08-24 02:04:54
【问题描述】:
如果我在select 子句中创建别名,那么我不能在where 子句中使用它,因为根据sql 查询的执行顺序,where 在select 之前。
但是我可以在select 子句中创建一个别名,并在having 子句中使用它,尽管having 在select 之前。
为什么会这样?
例如:
select type, (case when number>25 then 1 else 0 end) inc
from animals
where inc='1';
这行不通。但是,
select type, (case when number>25 then 1 else 0 end) inc
from animals
having inc='1';
这行得通。为什么会这样?
【问题讨论】:
-
这是标准的 MySQL 扩展。例如,您不能在 SQL Server 中引用
having中的列别名。
标签: sql group-by having having-clause