天天看點

Sql-Server資料庫單表查詢 4.3實驗課

今天更進一步的學習了資料庫單表查詢,涉及條件、分組、排序等

select [all|distinct] <目标表達式>
from <表名>
where <條件表達式>
group by 列名 [having <條件表達式>]
order by 列名 [asc|desc];
           
    • 1、選擇表的若幹列
    • 2、選擇表的元組(where)
      • 2.1 比較運算
      • 2.2 确定範圍和集合的查詢
      • 2.3 模糊查詢(like)
    • 3、排序(order by)
    • 4、聚集函數
    • 5、分組(group by)

1、選擇表的若幹列

查詢表時的目标表達式可以是列名、算術表達式、字元串和函數,也可以為查詢結果表中的列起别名(指定顯示出的列名)

select Sname 姓名,2021-Sage 出生年,'專業',upper(Sdept) 
from Student; --專業名轉大寫
-- 列名 (as) 别名
           
Sql-Server資料庫單表查詢 4.3實驗課

可以看到,T-SQL在查詢時若使用算術表達、字元串或函數,需要指定列擋名,否則顯示無列名

查詢某一列的資料時,可能會有重複元素,可使用distinct去重(預設為all,即全部顯示)

select Sno as 學号 from SC; --預設為all
select distinct Sno as 學号 from SC; --該列去重
           
Sql-Server資料庫單表查詢 4.3實驗課

2、選擇表的元組(where)

選擇條件的表達式在where關鍵字後填寫

2.1 比較運算

比較運算符有=、>、<、>=、<=、!=、<>、!>、!<

同時可使用not搭配上述運算符,相當于取反

select Sname,Sage,Sdept
from Student
where Sdept='CS'; 
--查詢CS專業的學生姓名和年齡
--字元串需要單引号,内容不區分大小寫,'CS'和'cs'效果相同
           
Sql-Server資料庫單表查詢 4.3實驗課

T-SQL中不支援使用not+比較運算符的結構

--查詢20歲及以下
/*select Sname,Sage
from Student
where Sage not > 20;*/ --不支援 not+比較運算符

select Sname,Sage
from Student
where Sage !> 20;
           
Sql-Server資料庫單表查詢 4.3實驗課

涉及空值時,不可使用 =null ,需 is null

使用 =null,相當于查找取值為null的元組,不是空值

2.2 确定範圍和集合的查詢

SQL語言中邏輯運算符為 and、or、not,相當于C語言中的 &&、||、!

使用 between ··· and ··· 可查詢屬性在指定範圍内的元組,包括兩端,可使用not取反

select Sname,Sage
from Student
where Sage between 19 and 21; --19~21歲之間,包括19和21
-- Sage>=19 and Sage<=21

select Sname,Sage
from Student
where Sage not between 19 and 21; --不在19~21歲之間
-- Sage<19 and Sage>21
           
Sql-Server資料庫單表查詢 4.3實驗課

使用 in 可查詢屬性在指定集合内的元組,可使用not取反

select Sname,Sdept
from Student
where Sdept in('CS','IS'); --專業是CS、IS
-- Sdept='CS' or Sdept='IS'

select Sname,Sdept
from Student
where Sdept not in('CS','IS'); --專業不是CS、IS
-- Sdept!='CS' and Sdept!='IS'
           
Sql-Server資料庫單表查詢 4.3實驗課

2.3 模糊查詢(like)

like+比對串,可使用not取反

即字元串比對查詢,使用通配符 %或_ 代替字元

  • %,代表任意長度的字元串(可以為0)
select Sname,Sdept
from Student
where Sname like '王%'; --王姓學生

select Sname,Sdept
from Student
where Sname not like '王%'; --非王姓學生
           
Sql-Server資料庫單表查詢 4.3實驗課
  • _,代表任意單個字元
select Sname,Sdept
from Student
where Sname like '歐陽_'; --歐陽姓三字學生

select Sname,Sdept
from Student
where Sname like '歐陽__'; --歐陽姓四字學生
           
Sql-Server資料庫單表查詢 4.3實驗課

資料庫字元集為ASCII時一個漢字需要兩個_;字元集為GBK時隻需要一個_

查詢Sql-Server目前字元集編碼方式可使用如下語句:

Sql-Server資料庫單表查詢 4.3實驗課

查詢結果為一整數,代表某種字元集的編号

936:簡體中文GBK

950:繁體中文BIG5

437:美國/加拿大英語

932:日文

949:韓文

866:俄文

65001:unicode UFT-8

% 和 _ 作為通配符,那麼當字元串中需要真正表示百分号和下劃線時,需要使用關鍵字escape定義轉碼字元

select Sname,Sdept 
from Student
where Sname like '王/%' escape'/'; --轉義%為普通的百分号
           
Sql-Server資料庫單表查詢 4.3實驗課

3、排序(order by)

order by用于對查詢結果進行排序,需指定排序的屬性名,asc升序、desc降序,預設為升序asc

select Sname,Sage,Sno
from Student
where Sdept in('cs','is')
order by Sage; --按年齡升序排序

select Sname,Sage,Sno
from Student
where Sdept in('cs','is')
order by Sage,Sno desc; --年齡相同時按學号(字元串)降序排序
           
Sql-Server資料庫單表查詢 4.3實驗課

4、聚集函數

聚集函數用于資料的統計功能,隻可用于select語句和group by的having語句

  • count(*):元組的個數
  • count(<列名>):一列中值的個數‘
  • sum(<列名>):一列值得總和(該列必須為數值型)
  • avg(<列名>):一列值的平均值(該列必須為數值型)
  • max(<列名>):一列值的最大值
  • min(<列名>):一列值得最小值

這些函數在使用時,可選擇統計該列distinct去重後或all所有,預設為all

select count(*) as 學生總數 from Student; --學生總數
select count(Sdept) from Student; --有專業的學生總數
select count(distinct Sno)  from SC; --選修了課得學生總數
select avg(Sage) as 平均年齡 from Student; --平均年齡
select max(Sage) as 最大年齡 from Student; --最大年齡
select min(Sage) as 最小年齡 from Student; --最小年齡
           
Sql-Server資料庫單表查詢 4.3實驗課

使用count(<列名>)查詢數量時,會依據元組的該屬性是否存在,是以會忽略空值,而使用count(*)查詢時不會忽略空值

5、分組(group by)

group by語句可将查詢結果按某一列或多列分組,同時可使用having語句搭配聚集函數對每組的資料進一步篩選

select Sdept,count(*) as 人數
from Student
group by Sdept; --各個專業的人數

select Sdept,count(*) as 人數
from Student
group by Sdept having count(*)>1 
order by 人數; --查詢專業人數大于1的專業,按升序排序
           
Sql-Server資料庫單表查詢 4.3實驗課

再複習一遍查詢語句的格式

select [all|distinct] <目标表達式>
from <表名>
where <條件表達式>
group by 列名 [having <條件表達式>]
order by 列名 [asc|desc];
           

SQL的查詢還是很有意思的,真強大