select中不能用别的列的别名吗
我想写一个select,其中一个字段是
case aaa
when 1 then a * b / 100
when 2 then a / (100 + b) * b
end
而其中的b是一个有点复杂的子查询,
我想写成
select
(select ccc from ddd where ......) as b,
case aaa
when 1 then a * b / 100
when 2 then a / (100 + b) * b
end
from ...
可是好像编译不过去,我必须写成下面这样吗
select
(select ccc from ddd where ......) as b,
case aaa
when 1 then a * (select ccc from ddd where ......) / 100
when 2 then a / (100 + (select ccc from ddd where ......) ) * (select ccc from ddd where ......)
end
from ...
谢谢了
[解决办法]
select ...
from ...
join (select ccc from ddd where ......) as b on ...
[解决办法]
简单的方法就是,在外面再套一层,这样:
select b,
case aaa
when 1 then a * b / 100
when 2 then a / (100 + b) * b
end
from
(
select
(select ccc from ddd where ......) as b,
...
from ...
)t
这样就简单多了哈
[解决办法]
如果你要单层调用,需要把产生别名的那个列用上,因为SELECT 里面,所有列都是“同时”产生,不是说左边先生成再生成后边的列。或者你在外层再包一层