sql语句中给列参数取别名及相关注意事项

1、使用双引号

select  count(*) "总数" from table;

2、使用单引号

select count(*) '总数' from table;

3、直接加别名,用空格隔开

select count(*) zongshu from table;

4、使用as关键字连接

select count(*) as zongshu from table;

5、使用=号连接(sqlserver使用)

select count(*) =zongshu from table;

注意事项

当我们使用某个表达式作为输出的一列时,我们无法再Where条件中直接使用该列作判断条件. 

例如下面的SQL语句:
 
select id, (c1 + c2) as s from t1  where s > 100 
  SQL Server 报错: "列名 s 无效" 

使用以下语句即可

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
 SELECT 语句的执行顺序
 1. from语句
 2. where语句(结合条件)
 3. start with语句
 4. connect by语句
 5. where语句
 6. group by语句
 7. having语句
 8. model语句
 9. select语句
 10. union、minus、intersect等集合演算演算
 11. order by语句

猜你喜欢

转载自www.cnblogs.com/chxmtl/p/11804251.html