CASE函數(shù)用法
語法:
1.Case...end 會生成一個新列,作用是判斷你指定字段的值或者范圍,然后得到一個用戶自定義的對應(yīng)的值
2.它并不會修改原始的表數(shù)據(jù),而只是修改結(jié)果集的顯示
3.case可以做等值判斷,也可以做范圍判斷。
4.做等值判斷的語法:
case 字段或者表達(dá)式
when 具體的值 then '用戶自定義值'
else 上面的when都不滿足就滿足else
end
5.做等值判斷的時候不能判斷null值
select StudentNo,StudentName,
case ClassId+1 --當(dāng)case后面接有字段或者表達(dá)式說這個case只能做行等值判斷
when 1 then '一期班'
when 2 then '二期班'
else '我不知道'
end,--case會生成一個新列,所以如果Case..end后面還有其它列,就必須先使用,進(jìn)行分割
case email
when NULL then '沒有填寫'
else email
end as 你要的列名
from Student
case ..end做范圍判斷
語法:
case --沒有接任何的列名或者表達(dá)式
when 表達(dá)式 then 值
。。。。
else 值
end
then后面的值的類型需要可以互換
when后面的表達(dá)式使用的字段可以是任意的,并不要求是同一個
select StudentName,
case
when BornDate>'2000-1-1' then '小屁孩' --如果不滿足這個when相當(dāng)于隱藏一個條件轉(zhuǎn)到下一個when
when BornDate>'1990-1-1' then '小青年'
when BornDate>'1980-1-1' then '小中年'
when sex='男' then '他是男的'
end,
case
when Email is null then '沒有填寫'
else Email
end
from Student
百分制轉(zhuǎn)換為素質(zhì)教育
select StudentNo,
case
when StudentResult>=90 then 'A'
when StudentResult>=80 then 'B'
when StudentResult>=70 then 'C'
when StudentResult>=60 then 'D'
when StudentResult is null then '沒有考試'
else 'E'
end AS 成績
from Result
--IF ELSE--
1.也有多重和嵌套
2.沒有{},用begin..end替代
3.if結(jié)構(gòu)里面必須有語句做處理
4.沒有所謂的true/false
5.()可以省略
if(1<>1)
begin
print 'aa'
print 'bb'
end
go
--計算office平均分?jǐn)?shù)并輸出,如果平均分?jǐn)?shù)超過60分輸出成績最高的三個學(xué)生的成績,否則輸出后三名的學(xué)生
declare @subjectName nvarchar(50)='office'--科目名稱
declare @subjectId int=(select SubjectId from Subject where SubjectName=@subjectName)--科目ID
declare @avg int--平均分
select @avg=(select AVG(StudentResult) from Result where SubjectId=@subjectId and studentresult is not null)
if @avg>=60
begin
print '成績不錯。輸入前三名:'
select top 3 * from Result where SubjectId=@subjectId order by StudentResult desc
end
else
begin
print '成績不好。輸入后三名:'
select top 3 * from Result where SubjectId=@subjectId order by StudentResult asc
end
--WHILE循環(huán)--
1.不有寫true/false
2.沒有{},只有begin..and
3.可以嵌套
4.也可以在循環(huán)體寫continue/break,continue可以中止當(dāng)前這一次,繼續(xù)下一次,break是跳出當(dāng)前這一層循環(huán)
---計算1-100之間所有奇數(shù)的和--
declare @i int=1
declare @sum int=0
while(@i<=100)
begin
if(@i%2!=0)
begin
set @sum+=@i
end
set @i+=1
end
print '和='+cast(@sum as char(4))
go
--如果office不及格的人超過半數(shù)(考試題出難了),則給每個人增加2分,循環(huán)加,直到不及格的人數(shù)少于一半。
declare @subjectName nvarchar(50)='office'--科目名稱
declare @subejctId int=(select subjectId from Subject where SubjectName=@subjectName)--科目ID
declare @totalNum int --總?cè)藬?shù)
set @totalNum=(select COUNT(*) from Result where SubjectId=@subejctId)
declare @unpassNum int---沒有及格人數(shù)
select @unpassNum =COUNT(*) from Result where SubjectId=@subejctId and StudentResult<70
print @totalnum
print @unpassnum
while(@totalNum/2<@unpassNum)
begin
update Result set StudentResult+=5 where SubjectId=@subejctId and StudentResult <=95
--再一次計算不及格人數(shù)
select @unpassNum =COUNT(*) from Result where SubjectId=@subejctId and StudentResult<70
end
go
declare @subjectName nvarchar(50)='office'--科目名稱
declare @subejctId int=(select subjectId from Subject where SubjectName=@subjectName)--科目ID
declare @totalNum int --總?cè)藬?shù)
set @totalNum=(select COUNT(*) from Result where SubjectId=@subejctId)
declare @unpassNum int---沒有及格人數(shù)
select @unpassNum =COUNT(*) from Result where SubjectId=@subejctId and StudentResult<70
print @totalnum
print @unpassnum
while(1=1)
begin
if((select COUNT(*) from Result where SubjectId=@subejctId and StudentResult<70)>@totalNum/2)
update Result set StudentResult+=4 where SubjectId=@subejctId and StudentResult<=96
else
break
end
---變量賦值
---局部變量--
語法
declare @名稱 類型=初始值
declare @name varchar='aaaa' --如果字符串沒有指定長度,那么長度就是1
print @name
兩種賦值方式:在sql中為變量賦值必須使用set/select,而不能直接使用 變量=值
set:側(cè)重于直接給一個具體的值
select:側(cè)重于通過查詢賦值
go
--查詢比林思大的學(xué)員信息
select * from Student where BornDate<(select BornDate from Student where StudentName='林思')
go
declare @time datetime
--set后面如果是sql語句,那么必須接完整的 獨(dú)立子查詢
set @time=(select BornDate from Student where StudentName='林思')
--使用select賦值,如果后面是sql語句,可以是獨(dú)立子查詢,也可以省略select關(guān)鍵字
select @time= BornDate from Student where StudentName='林思'
print @time
使用set/select賦值的共同點(diǎn)
1.都能直接給值
2.如果后面的sql語句是一個完整Sql語句,那么兩者沒有任何的區(qū)別
區(qū)別:
go
declare @age int=100,@name varchar(50)='aaa'
1.set一次只能為一個變量賦值,而select可以一次為多個變量賦值
set @age=20 ,@name='張三'
select @age=20 ,@name='張三'
2.set進(jìn)行賦值的時候如果sql語句返回多行一列值,那么:子查詢返回的值不止一個。當(dāng)子查詢跟隨在 =、!=、<、<=、>、>= 之后,或子查詢用作表達(dá)式時,這種情況是不允許的。但是如果是select賦值且sql語句省略了select.那么會得到最后一個值
set @name=(select StudentName from Student)
select @name= StudentName from Student
3.如果sql語句返回null值,那么set會成為null值而select會保留原始值
set @name=(select StudentName from Student where StudentNo=100)
select @name= StudentName from Student where StudentNo=100
print @name
print @age
兩種輸出方式:
select:以結(jié)果集的方式輸出
print :以文本形式輸出,每一個print單獨(dú)占據(jù)一行,且永遠(yuǎn)只能輸出一個值,它是在服務(wù)器端輸出的
select * from Student
print 'aa' +'bb'
查詢參加最近一次“office”考試成績最高分和最低分
select MAX(StudentResult),MIN(StudentResult)
from Result
where SubjectId=(select subjectid from subject where subjectname='office')
and ExamDate=(select max(examdate) from result where subjectid=(select subjectid from subject where subjectname='office'))
declare @subjectname nvarchar(50)='office' --科目名稱
go
declare @subjectname nvarchar(50)='office' --科目名稱
declare @subjectId int --科目 ID
set @subjectId=(select subjectid from subject where subjectname=@subjectname)
declare @time datetime --最近一次考試日期
select @time=MAX(examdate) from Result where SubjectId=@subjectId
查詢
select MAX(StudentResult),MIN(StudentResult) from Result where SubjectId=@subjectId and ExamDate=@time