SQL Server入門二

SQL Server目錄:

一、連接池
二、模糊查詢like
三、通配符:%,_,[],^
1.%通配符:匹配0-n個任意字符
2._通配符,表示匹配任意1個字符
3.’^‘通配符:非,不是
4.’[]'通配符,表示匹配一定範圍內的,任意1個字符
四、聚合函數
五、select 完整語序
1.order by排序
2.group by分組查詢
3.distinct去除重複項
4.分組查詢原則
5.給列起別名
6.where子句(條件查詢)
7.top關鍵字:查詢前若干條語句

六、union聯合查詢:用來把兩個select查詢結果集合併成一個結果集
七、字符串處理函數
八、子查詢:把1個查詢結果在另一個查詢中使用

SQL Server正文:

一、連接池:

1.當執行到Close()方法時,ADO.NET會把connection對象放入連接池
2.連接池不屬於SQL Server數據庫,連接池是有由應用程序來維護的。
3.當應用程序退出時,連接池纔會銷燬。
4.如果連接對象長時間不用,連接對象纔會銷燬。
5.只有連接字符串完全相同,才能使用連接池裏的對象。

DataSet就是個臨時數據庫
DataTable就是個臨時數據表

二、模糊查詢 like

 select * from TbStudent where stuName = '劉備'
 select * from TbStudent where stuAge>=20 and stuAge<=30

三、通配符:%,_,[],^

1.%通配符:匹配0-n個任意字符

select * from TbStudent where stuName like '%喬'

2._通配符,表示匹配任意1個字符
–查找1:名‘喬’,並且名只有2個字的人

select * from TbStudent where stuName like '_喬'

–查找2:名字裏面有‘鏡’字的學生信息

select * from TbStudent where stuName like '%鏡%'

–查找3:名字裏面沒有‘喬’字的學生信息

 select * from TbStudent where stuName not like '%喬%'

3.’^'通配符:非,不是

select * from TbStudent where stuName like '%^喬%'-- 錯誤寫法,原因?

正確寫法:
--查詢不是16級的學生
select * from TbStudent where stuNumber not like '16%'
--^通配符:必須和[]搭配使用
select * from TbStudent where stuNumber like '1[^6]%'

4.’[]'通配符,表示匹配一定範圍內的,任意1個字符
(1)%匹配0-N個任意字符與’[]'配合使用
練習一:查詢姓‘諸葛’或者姓‘周’的所有記錄

  方法一:
  select * from TbStudent where stuName like '諸葛%' or stuName like '周%'
  
  方法二:
  select * from TbStudent where stuName like '[諸葛,周]%'

(2)通配符[],永遠只去匹配1個字符
練習二:查詢14-16級的所有同學信息

--錯誤做法:select * from TbStudent where stuNumber like '[14,15,16]%'

正確做法:
use Test
select * from TbStudent where stuNumber like '1[4-6]%'
select * from TbStudent where stuNumber like '1[4,5,6]%'

(3)^通配符:必須和[]搭配使用
練習三:查詢不是16級的學生

  方法一:
select * from TbStudent where stuNumber not like '16%'

方法二:
select * from TbStudent where stuNumber like '1[^6]%'

ps:^不是標準SQL通配符,它只是 SQL Server支持的,其他DBMS都不支持該符號
我們一般優先使用not like,儘量不用^

--練習:查詢名字裏沒有‘喬’字的學生
select * from TbStudent where stuName not like '%喬%'
--錯誤:select * from TbStudent where stuName like '%[^喬]%'

(4) 匹配特殊字符:使用[]做轉義
假如人名裏就有’%’
–查詢名字裏有’%'的學生
select * from TbStudent where stuName like ‘%[%]%’

四、聚合函數

max(),min(),avg(),sum(),count(),count-big()

select * from TbStudent
--Max()
select MAX(stuAge) from TbStudent
--Min()
select MIN(stuAge) from TbStudent 
--Avg()
select AVG(stuAge) from TbStudent
--sum()
select sum(stuAge) from TbStudent

count()統計記錄條數*
count()的字段如果爲NULL,是不會被計算在內的

select count(*) from TbStudent
select count(stuName) from TbStudent
select count(stuId) from TbStudent
select count(stuAge) from TbStudent

count()記錄條數時,推薦使用主鍵或者1,作爲被count對象(效率高)

select count(1) from TbStudent
select count(stuAge) from TbStudent

avg()時,如果有記錄在該字段上爲NULL,這條記錄是不會被統計在內的

–這5個爲什麼叫聚合函數
–不管是多少條記錄,經過這5個函數的運算後,都只能得到1個數。

--注意:
--錯誤寫法:select stuName,AVG(stuAge) from TbStudent
--正確寫法:select MAX(stuAge) as 最大值,MIN(stuAge) as 最小值,AVG(stuAge) as 平均值 from TbStudent

空值null處理

如果判斷一個字段的值是不是null,需要使用is關鍵字,不能用=

--例一:查詢籍貫是null的所有同學

select * from TbStudent where stuAddress is null


--例二:查詢籍貫不是null的所有同學
select * from TbStudent where stuAddress is not null

–注意:not like,is not

--例一:查詢所有同學信息,如果籍貫是null,就用'未知'代替
--ISNULL(原始數據,代替值)函數
select stuName,stuNumber,ISNULL(stuAddress,'未知')from TbStudent

--例二:查詢所有數學成績爲null的學生
select * from TbStudent where stuMath is null

--例三:查詢所有學生的考試成績,如果爲null,則用'缺考'代替

--轉義失敗:select stuName,stuNumber,ISNULL(stuMath,'缺考')from TbStudent

–原因:stuMath是int類型,‘缺考’是varchar類型,所以不允許填充到同1列上
–數據表中任何1列,都只能存儲同一種數據類型的數

–SQL裏的null與任何數據做運算,結果還是null

--例四:查詢所有的學生的數學成績,並且給所有人的數據成績+10分
select stuName,stuNumber,stuMath+10 as '數學' from TbStudent
--數學成績爲null的依然是null

cast:轉義類型

查詢所有同學信息,如果籍貫是null,就用’未知’代替
–ISNULL(原始數據,代替值)函數

select stuName,stuNumber,ISNULL(CAST(stuMath as varchar(4)),'缺考')from 

**–

五、select 完整語序

語法:
–1.from [表名]
–2.where 條件
–3.group by 列
–4.having 篩選
–5.select [字段列表]
–5.1->[字段列表] 5.2->distinct 去除查詢結果中完全重複的記錄 5.3->top
–6.order by 列
–order by可以使用列的別名,是因爲在第5步纔給列起了別名。**

select * from TbStudent where stuAddress = '竹園'

1.order by排序

語法:order by[字段名] asc/desc
select * from TbStudent

--例1:查詢每個同學的數學成績,按照升序排列
select stuName,stuNumber,stuMath from TbStudent order by stuMath asc

–根據數學排序,如果數學成績一樣,再根據英語排序
–一個select語句,永遠只能有一個Order by子句

select stuName,stuNumber,stuMath,stuEnglish from TbStudent order by stuMath asc,stuEnglish asc

注意:如果不寫asc或者desc,默認的就是asc

select stuName,stuNumber,stuMath,stuEnglish from TbStudent
order by stuMath

–order by後邊還可以跟表達式

--例:查詢所有學生的平均成績,按降序排列  /2.0得出的結果有小數
select stuName,(stuMath+stuEnglish)/2.0 as 平均分 from TbStudent
order by (stuMath+stuEnglish)/2.0 desc

–order by後邊,可以使用列的別名,但是where子句中不能使用列的別名

--例:查詢所有學生的平均成績,按降序排列,並且顯示平均分大於60分的
select stuName,(stuMath+stuEnglish)/2.0 as '平均分' from TbStudent
where (stuMath+stuEnglish)/2.0>60
order by 平均分

2.group by分組查詢

查詢男生女生各有多少人
–group by[字段名]
–分組的目的:做統計
use Test

--分組後的count,是統計各個分組內的記錄條數,而不是整個表的記錄條數
--有多少個分組,就會count出多少個值

select stuGender,COUNT(1) from TbStudent group by stuGender

3.distinct去除重複項

select distinct stuGender from TbStudent

–練習1:查詢每個地方有多少名同學

select stuaddress,COUNT(1) from TbStudent group by stuAddress

–練習2:根據各個地區人數排序

select stuaddress,COUNT(1) as 人數 from TbStudent group by stuAddress
order by 人數

4.分組查詢原則:
在分組查詢中,select後邊的字段列表必須與group by中字段名一一對應
–除非它出現在了聚合函數裏

select COUNT(1) as 人數,AVG(stuMath) as 數學平均分 from TbStudent

–練習1:查詢每個班數學和英語的平均分

select stuclassId,avg((stuMath+stuEnglish)/2)from TbStudent group by stuClassId

–練習2:查詢每個班的男生數學和英語的平均分

select stuclassId,avg((stuMath+stuEnglish)/2)from TbStudent where stuGender = 1 group by stuClassId

–練習3:查詢每個班的男生數學和英語的平均分

select stuClassId,avg((stuMath+stuEnglish)/2)from TbStudent where stuGender = 1 group by stuClassId

select '男',stuclassId,avg((stuMath+stuEnglish)/2)from TbStudent where stuGender = 1 group by stuClassId

–練習4:查詢每個班的男生數學和英語的平均分,並且只顯示平均分高於60分的

   --錯誤寫法
    select stuclassId,avg((stuMath+stuEnglish)/2) from TbStudent
    where stuGender = 1 and avg((stuMath+stuEnglish)/2)>60 group by stuClassId

–上邊的寫法爲什麼不行?
where條件,只能篩選表裏出現的原始字段數據

–如果要根據分組統計後出現的數據進行篩選,要使用having子句
–having只能跟在group by搭配使用

 --正確寫法
 select stuclassId,avg((stuMath+stuEnglish)/2)from TbStudent
 where stuGender = 1 group by stuClassId
 having avg((stuMath+stuEnglish)/2)>60

–總結:
1.列的別名只能用在order by之後,其他地方不能用
2.where只能對原始表中出現的數據做篩選時使用。
3.having只能對分組後的結果做篩選,having中不能出現爲參與分組的列

ps:錄入數據時,如果字段是bit類型的,給的值可以是0/1,也可以是’true’/'false’

insert into TbStudent(stuName,stuGender) values('魯達','true')

5.給列起別名

select stuName as '姓名',--第一種起別名方法
stuName 學號,--第2種起名方法
性別 = stuGender--第3種起別名方法
from TbStudent

1.查詢學生是否是黨員

select stuName,stuNumber,stuGender,是否黨員 = '是'from TbStudent
select * from TbStudent

2.查詢出學生出生年份

select stuName,stuNumber,出生年份=2019-stuAge from TbStudent

3.構造新的無字段列

select *,'未婚' from TbStudent

6.where子句(條件查詢)
where子句一定要放在被查詢的表名的後面
1.查找性別爲女的所有數據,SQL Server中判等使用=

select * from TbStudent where stuGender = 0

2.大小範圍比較,關係運算:=,>,<,>=,<=,!=(不等於)

select * from TbStudent where stuAge>20and stuAge<25

3.邏輯運算 and or not

select * from TbStudent where stuClassId!=2

4.範圍比較,between…and包含兩端的數字
–between…and…是用來比較連續的值

select * from TbStudent where stuAge between 20 and 25

–查詢所有1,2,3班的學生信息
–一個查詢可以實現多重實現

select * from TbStudent where stuClassId = 1 or stuClassId=2 or stuClassId=3

–查詢1,2,4班學生信息 使用in關鍵字 查詢離散值

select * from TbStudent where stuClassId in(1,2,4)

–不在這3個班的同學

select * from TbStudent where stuClassId not in(1,3,4)

7.top關鍵字:查詢前若干條語句

查詢前5條數據

select top(5)*from TbStudent where stuClassId in(1,2)

六、union聯合查詢:用來把兩個select查詢結果集合併成一個結果集

select stuName,stuAge from TbStudent union
select clsName,clsNumber from TbClass

1.union查詢,多個查詢的列,個數必須相同
2.union查詢,多個查詢的列,各個列的數據類型必須一一對應
3.union會自動剔除完全重複記錄,如果需要保留重複記錄,使用union all

select stuName,stuAge from TbStudent union all
select stuName,stuAge from TbStudent 

–練習1:統計銷售明星

   select saleMan,SUM(number) from SaleRecords group by saleMan

–練習2:要求,在一個結果集裏查詢出學生的英語最高分,最低分,平均分

select MAX(stuEnglish) as 最高分,
MIN(stuEnglish) as 最低分,
AVG(stuEnglish) as 平均分
from TbStudent

select ‘最高分’ as 類別,MAX(stuEnglish) as 分數 from TbStudent union all
select ‘最低分’ as 類別,min(stuEnglish) as 分數 from TbStudent union all
select ‘平均分’ as 類別,avg(stuEnglish) as 分數 from TbStudent

七、字符串處理函數

1.顯示數值對應的ASCII
select CHAR(47)

–2.顯示ASCII對應的數值

select ASCII('a')

–3.查詢子串在長字符串中首次出現的索引位置
–注意:SQL中,索引下標是從1開始計數的,不是從0計數

select CHARINDEX('o','I love you')

–4.取子串,從左邊連續截取3個字符

select left('abcdef',3)

–5.從右邊連續截取3個字符

select right('abcdef',3)

–6.substring()從第2個下標開始截取(包含2),連續截取4個

select SUBSTRING('abcdef',2,4)

–7.lower()大寫轉小寫

select LOWER('ABC')

–8.upper()小寫轉大寫

select UPPER('and')

–9.剔除字符串左右兩端空格
–ltrim剔除左邊空格
–rtrim剔除右邊空格

select rtrim(LTRIM('   劉亞萌    ')) + ',我來賣個萌'

–10.子串的替換,替換所有的能匹配的子串

select REPLACE('東哥真帥','帥','有愛')

–11.只替換固定位置的子串:stuff(字符串,起始索引位置,替換長度,替換子串)

select STUFF('你一定能行',2,2,'真的')

–12.字符串翻轉

print reverse('天安門')
select REVERSE('天安門')

–13.space添加空格

select '劉亞萌' + space(5) + '別瞌睡'

–注意:這些函數的參數,都可以使用表中的字段名來代替

–14.日期類型函數
–SQL2005及以下版本,日期類型只有DATETIME
–SQL2008及以上版本,日期類型有3個:datetime,date,datetime2

–三者區別:
date:只存儲年月日,所佔空間最小
datetime:存儲年月日時分秒毫秒。時間格式:yyyy-MM-dd HH:mm:ss:fff,3個f,精確到1毫秒(ms),示例2019-04-15 19:58:16.433。
字段類型使用GETDATE()
datetime2:存儲年月日時分秒毫秒微秒。時間格式:yyyy-MM-dd HH:mm:ss.fffffff,7個f,精確到0.1微秒,示例:2019-04-15 19:59:2888090
字段類型使用:SYSDATETIME()

DATEADD日期函數

--DateAdd(datepart,number,date)
select DATEADD(DAY,200,GETDATE())
select DATEADD(MONTH,200,GETDATE())
select DATEADD(YEAR,200,GETDATE())
select DATEADD(HOUR,200,GETDATE())
select DATEADD(MINUTE,200,GETDATE())
select DATEADD(SECOND,200,GETDATE())

select * from TbStudent
select stuName,DATEADD(YEAR,stuAge,getdate()) from TbStudent

datediff計算時間差

--datediff(datepart,date1,date2)
--date1是較早的時間,date2是較新的時間
select 
DATEDIFF(MONTH,'1999-9-9',getdate())

–如果student表中有birthday字段,如何獲取每人的年齡

select DATEDIFF(year,birthday,GETDATE())from student

day(),month(),year()

select DAY(getdate())  --今天日期
select MONTH('1998-10-18')--月份 10
select DATEPART(year,getdate())--年2019

update更新,刪除

–語法:
–update [表名] set …where
–練習1:把學生的年齡改爲20歲

update TbStudent set stuAge = 20,stuEnglish = 60

–練習2:把英語成績爲null的學生,改成60分

update TbStudent set stuEnglish = 60 where stuEnglish is null
select * from TbStudent

–練習3:把女生的年齡改小了1歲

update TbStudent set stuAge = stuAge - 1 where stuGender = 0

–delete刪除數據
–語法:
–delete from[表名] where…

–練習1:把家是菊園的同學記錄刪除掉

delete from TbStudent where stuAddress = '菊園'
select * from t

–練習2:刪除表中所有記錄

delete from callRecords
truncate table TbClass callRecords

–區別:
1.delete刪除了數據以後,自增的字段不會自動歸0.
truncate刪除了數據以後,表的自增字段會自動歸0.
2.delete可以使用where子句,truncate不能使用,它只能刪除表裏的所有數據。
3.truncate刪除的數據不能恢復,delete刪除的數據可以在一定條件下,根據日誌文件的記錄來恢復。
原因:delete刪除數據時,系統會自動在.ldf中寫下日誌。而truncate刪除時,是不做日誌的。
4.truncate刪除時速度非常快,delete刪除時要慢一點

–練習:把所有姓’劉’的同學,改成姓’郭’

update TbStudent set stuName=REPLACE(stuName,'劉','郭')where stuName like '劉%'

case語句
case語法1:

--case when condition1 then returnValue1
--when condition2 then returnValue2
--when condition3 then returnValue3
--......
--else defaultValue
--end as[列別名]

–注意:
– 1.CASE語句就是構造出1個新的列
– 2.使用case語句必須有end,但是可以不起別名
– 3.所有的返回值類型必須一致
– 4.case語句的作用類似於C#中的if…else…

–練習1:查詢每個學生的學號、姓名、最好成績

--select stuName,stuNumber,
--case when stuMath>stuEnglish then stuMath
--else stuEnglish
--end as 最優成績
--from TbStudent

–練習2:查詢學生的數學成績,以優良中差4級顯示

select stuNumber,stuName,
case when stuMath>=90 then '優'
when stuMath>=80 then '良'
when stuMath>=70 then '中'
else '差'
end as 數學成績
from TbStudent

case語法2

--case表達式 when value1 then returnValue1
--           when value2 then returnValue2
--           ...
--           else end as[別名]
--類似於C#中的switch...case  只能做離散值的判斷

–練習1:查詢學生信息,顯示姓名、學號、性別,
–其中性別一欄如果是0,顯示“美女”,1顯示“帥哥”

--方法1 應用更廣泛
select stuName,stuNumber,
case when stuGender = 0 then '美女'
when stuGender = 1 then '帥哥'
end as性別
from TbStudent

--方法2
select stuName,stuNumber,
case stuGender
when 0 then '美女'
when 1 then '帥哥'
else '人妖'
end as 性別
from TbStudent 

八、 子查詢:把1個查詢結果在另一個查詢中使用(把一個查詢語句做一個結果集使用)

–查詢:班級號是2,名字裏帶‘喬’字的同學信息

select * from TbStudent where stuClassId = 2 and stuName like '%喬%'

–子查詢版
–當使用子查詢時,必須給子查詢語句最後的結果集起別名

select stuName,stuNumber,stuAddress from
(select*from TbStudent where stuClassId=1) as tb1 where stuName like '%喬'

–練習1:查詢’13級.net班’所有學生信息

select stuName,stuNumber,stuAddress from TbStudent
where stuClassId = 
(select clsId  from TbClass where clsId='1')

–練習2:查詢所有‘14級數據庫班’和‘13嵌入式班’的所有同學的信息

select stuName,stuNumber,stuAddress from TbStudent
where stuClassId in
(select clsId from TbClass WHERE clsName='14級數據庫班'or clsName='13嵌入式班')

–練習3:刪除所有姓’諸葛’的同學

--方法1
delete from TbStudent where stuName like '諸葛%'

--方法2
delete from TbStudent where stuId in
(select stuId from TbStudent where stuName like'諸葛%')

子查詢高級應用分頁查詢

–每頁顯示5條記錄

 --查詢第1頁
 select top(5) * from TbStudent
 --查詢第2頁
 select top(5) * from TbStudent where stuId not in
 (select top(1*5)stuId from TbStudent order by stuId asc) order by stuId ASC

–思路:排除前多少頁條記錄,去剩下記錄的前幾

 --第1種方法:效率低
 select top(M) * from TbStudent
 where stuId not in
 (
   select top((N-1)*M)stuId from TbStudent
 )
 

 --第2種方法
查詢第N頁,每頁顯示M條  規律
 --select * from
 --(select *,ROW_NUMBER() over (order by stuId desc)as rowNumber from TbStudent)
 --as 別名
 --where 別名.rowNumber between (N-1)M+1 and N*M




 select *,ROW_NUMBER() over(order by stuId desc)as rowNumber from TbStudent
 --查詢‘第2頁’
 --注意:當子查詢的結果集作爲主查詢的數據源是,必須給子查詢的結果集起別名
 select * from
 (select *,ROW_NUMBER() over(order by stuId desc)as rowNumber from TbStudent)
 as tb1
 where tb1.rowNumber between 1*5+1 and 2*5
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章