sql别名何时使用

http://www.newxing.com/Tech/Database/MSSQL/91.html------------------同意

http://hi.baidu.com/hellofrancis/item/1db89f0a2673f1036d904805-----这个说法好像不对


别名的有效性也是遵循sql执行顺序的,你不能在执行别名命名语句之前就使用它。


例如下面的SQL语句: 

select id, (c1 + c2) as s
from t1
where s > 100 
  SQL Server 报错: "列名 s 无效"


当然,写成 
select id, (c1 + c2) as s
from t1
where (c1 + c2) > 100 

 就没问题了.


 
  可是当表达式复杂时就很繁琐了.
 
  有没有可以在Where中使用这样的列名的办法?
  或者有什么其他办法可以解决这类问题呢? 

解决方法:


select t2.*
from (select id, (c1 + c2) as c from t1) t2
where c > 100
 
  --或者
 
select t2.*
from (select id, c = c1+c2 from t1) t2
where c > 100


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章