如果我在select
子句中创建了别名,那么我不能在where
子句中使用它,因为根据sql查询的执行顺序where
在select
之前。与别名之间的区别
但我可以创建select
子句中的别名,虽然having
来select
使用它之前在having
条款。
这是为什么?
例:
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';
This works。为什么这样?
这是标准的MySQL扩展。例如,你不能在SQL Server的'having'中引用列别名。 –