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語句

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM