SQL進階-(1)


--CASE函數用法--

--語法:

--1.Case...end會生成一個新列,作用是判斷你指定字段的值或者范圍,然后得到一個用戶自定義的對

 

應的值

--2.它並不會修改原始的表數據,而只是修改結果集的顯示

--3.case可以做等值判斷,也可以做范圍判斷。

--4.做等值判斷的語法:

--case 字段或者表達式

--  when 具體的值 then '用戶自定義值'

--  else 上面的when都不滿足就滿足else

--end

--5.做等值判斷的時候不能判斷null

select StudentNo,StudentName,

case ClassId+1 --case后面接有字段或者表達式說這個case只能做行等值判斷

 when 1 then '一期班'

 when 2 then '二期班'

  else '我不知道'

end,--case會生成一個新列,所以如果Case..end后面還有其它列,就必須先使用,進行分割

case email

 when  NULL  then '沒有填寫'

 else email

end as 你要的列名

 from Student

--case ..end做范圍判斷

--語法:

--case --沒有接任何的列名或者表達式

-- when 表達式 then

-- 。。。。

-- else  值  

--end

--then后面的值的類型需要可以互換

--when后面的表達式使用的字段可以是任意的,並不要求是同一個

select StudentName,

case

  when BornDate>'2000-1-1' then '小屁孩' --如果不滿足這個when相當於隱藏一個條件轉到下一個

 

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

 

--百分制轉換為素質教育

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結構里面必須有語句做處理
--4.沒有所謂的true/false
--5.()可以省略
--if(1<>1)
-- begin
--  print 'aa'
--  print 'bb'
-- end
go
--計算office平均分數並輸出,如果平均分數超過60分輸出成績最高的三個學生的成績,否則輸出后三名的學生
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循環--

--1.不有寫true/false

--2.沒有{},只有begin..and

--3.可以嵌套

--4.也可以在循環體寫continue/break,continue可以中止當前這一次,繼續下一次,break是跳出當前這一層循環

 

---計算1-100之間所有奇數的和--

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不及格的人超過半數(考試題出難了),則給每個人增加2,循環加,直到不及格的人數少於一半。

declare @subjectName nvarchar(50)='office'--科目名稱

declare @subejctId int=(select subjectId from Subject where SubjectName=@subjectName)--科目ID

declare @totalNum int --總人數

set @totalNum=(select COUNT(*) from Result where SubjectId=@subejctId)

declare @unpassNum int---沒有及格人數

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

--再一次計算不及格人數

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 --總人數

set @totalNum=(select COUNT(*) from Result where SubjectId=@subejctId)

--declare @unpassNum int---沒有及格人數

--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:側重於直接給一個具體的值

--select:側重於通過查詢賦值

go

--查詢比林思大的學員信息

select * from Student where BornDate<(select BornDate from Student where StudentName='林思')

go

declare @time datetime

--set后面如果是sql語句,那么必須接完整的 獨立子查詢

--set @time=(select  BornDate from Student where StudentName='林思')

--使用select賦值,如果后面是sql語句,可以是獨立子查詢,也可以省略select關鍵字

select @time=  BornDate from Student where StudentName='林思'

print @time

--使用set/select賦值的共同點

--1.都能直接給值

--2.如果后面的sql語句是一個完整Sql語句,那么兩者沒有任何的區別

go

--區別:

declare @age int=100,@name varchar(50)='aaa'

--1.set一次只能為一個變量賦值,select可以一次為多個變量賦值

--set @age=20 ,@name='張三'

--select @age=20 ,@name='張三'

--2.set進行賦值的時候如果sql語句返回多行一列值,那么:子查詢返回的值不止一個。當子查詢跟隨在 =!=<<=>>= 之后,或子查詢用作表達式時,這種情況是不允許的。但是如果是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:以結果集的方式輸出

--print :以文本形式輸出,每一個print單獨占據一行,且永遠只能輸出一個值,它是在服務器端輸出的

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

 


免責聲明!

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



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