如果我在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'中引用列別名。 –