--IF ELSE
--特点:
--1.没有{},使用begin...end
--2.有多重和嵌套
--3.if必须有处理语句,如果只有一句,也可以不使用begin..end,只包含后面一句
--4.没有true/false的概念,只能使用关系运算符的条件表达式
--5.if后面的括号可以没有
--计算office平均分数并输出,如果平均分数超过60分输出成绩最高的三个学生的成绩,否则输出后三名的学生
go
declare @subjectname nvarchar(50)='office' --科目名称
declare @subjectId int=(select subjectId
from subject where subjectname=@subjectname) --获取科目ID
declare @avg int --科目平均分
set @avg=(select AVG(StudentResult) from Result
where SubjectId=@subjectId and StudentResult is not null) --获取指定科目的平均分
--做判断
if (@avg>=95)
begin
print '成绩不错。输出前三名'
select top 3 * from Result where SubjectId=@subjectId
and StudentResult is not null order by StudentResult desc
end
else
begin
print '成绩不好。输出后三名'
select top 3 * from Result where SubjectId=@subjectId
and StudentResult is not null order by StudentResult
end
-------------
go
declare @name nvarchar(50)='office'--科目名称
declare @id int =(select SubjectId from Subject where SubjectName =@name )--科目ID
declare @avg int = (select AVG (StudentResult ) from Result
where SubjectId =@id and StudentResult is not null )--获取指定科目的平均分
--做判断
if(@avg >=95)
begin
print '成绩不错,输出前三名'
select top 3*from Result where SubjectId =@id and StudentResult is not null
order by StudentResult desc
end
else
begin
print '成绩不好,输出后三名'
select top 3 *from Result where SubjectId =@id and StudentResult is not null
order by StudentResult asc
end