關(guān)于SQL Server查詢語句的使用
一.查詢第二個字母是t或者a的雇員的全部信息
select *
from employees
where firstname like '_[t,a]%'
注意:在sql中%表示字符串,所以不可像matlab一樣用其注釋,兩個雙斜線好像也不行,/**/可以,有網(wǎng)友說sql單行注釋為--
二.更改字段名
select '名字' = firstname ,'姓氏' = lastname
from employees
where firstname like '_[t,a]%'
或者
select firstname as '名字' , lastname as '姓氏'
from employees
where firstname like '_[t,a]%'
三.top關(guān)鍵字
/*檢索出符合條件的前70%條記錄*/
select top 70 percent firstname as '名字' , lastname as '姓氏'
from employees
where firstname like '_[t,a]%'1 /*檢索出符合條件的前2條記錄*/
select top 2 firstname as '名字' , lastname as '姓氏'
from employees
where firstname like '_[t,a]%'
四.union關(guān)鍵字
注意:標準sql只提供了并操作,未提供交(intersection)和差(minus)操作。
select *
from employees
where title = 'Sales Manager'
union
select *
from employees
where address is not null
顯示:
服務器: 消息 8163,級別 16,狀態(tài) 4,行 1
不能以 DISTINCT 方式選擇 text、ntext 或 image 數(shù)據(jù)類型。
select *
from employees
where title = 'Sales Manager'
union all
select *
from employees
where address is not null
查詢分析其中的分析查詢(對號)是看下是否有語法錯誤(ctrl + F5),執(zhí)行查詢(右三角)(F5)是顯示結(jié)果的若有語法錯誤則不執(zhí)行。
五.compute關(guān)鍵字
compute子句需要以下信息:
可選的By關(guān)鍵字可按對一列計算指定的行聚合
行聚合函數(shù):sum,avg,min,max,count
要對其執(zhí)行行聚合函數(shù)的列
當compute帶有可選的By子句時,符合select條件的每個組都有兩個結(jié)果集:
每個組的第一個結(jié)果集是明細行集,其中包含該組的選擇列表信息
每個組的第二個結(jié)果集有一行,其中包含該組COMPUTE子句中所指定的聚合函數(shù)的小記
select sex,sclass,score
from student
order by sex
compute sum(score) by sex

注意:order by是必須的,并且 compute by后的參數(shù)應該在order by后的參數(shù)中出現(xiàn)過
select sex,sclass,score
from student
compute sum(score)

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信