into Admin
select 'a','a'
union all
select 'a','a'
union all
select 'a','a'
union all
select 'a',null
?
?
12.CASE函数用法
?
相当于switch case---
c#中的switch...case只能做等值判断
这可以对字段值或者表达式进行判断,返回一个用户自定义的值,它会生成一个新列
2.要求then后面数据的类型一致
1.第一种做等值判断的case..end
case 字段或者表达式
when .值..then .自定义值
when .值..then .自定义值
.....
else 如果不满足上面所有的when就满足这个else
end
--显示具体班级的名称
select StudentNo,StudentName,
case ClassId --如果case后面接有表达式或者字段,那么这种结构就只能做等值判断,真的相当于switch..case
when 1 then '一班'
when 2 then '2班'
when 3 then '3班'
when null then 'aa' --不能判断null值
else '搞不清白'
end,
sex
from Student
--2.做范围判断,相当于if..else,它可以做null值判断
--case --如果没有表达式或者字段就可实现范围判断
-- when 表达式 then 值 --不要求表达式对同一字段进行判断
-- when 表达式 then 值
-- .....
--else 其它情况
--end
select StudentNo,StudentName,
case
when BornDate>'2000-1-1' then '小屁孩'
when BornDate>'1990-1-1' then '小青年'
when BornDate>'1980-1-1' then '青年'
--when Sex='女' then '是女的'
when BornDate is null then '出生不详'
else '中年'
end
from Student
--百分制转换为素质教育 90 -A 80--B 70 --C 60 --D <60 E NULL--没有参加考试
select StudentNo,SubjectId,
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 成绩,
ExamDate
from Result
?
?
?
13.IF ELSE语法
?
1,.没有{},使用begin..end.如果后面只有一句,可以不使用begin..end包含
2.没有bool值,只能使用关系运算符表达式
3.也可以嵌套和多重
4.if后面的()可以省略
?
?
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) --获取平均分
print @avg
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 ?
? end?
?
?
14.WHILE循环语法
?
没有{},使用begin..end
没有bool值,需要使用条件表达式
可以嵌套
也可以使用break,continue
?
?
go
declare @subjectName nvarchar(50)='office' --科目名称
declare @subjectId int--科目ID
declare @classid int =(select classid from Subject where SubjectName=@subjectName) --查询当前科目属于那一个班级
set @subjectId=(select SubjectId from Subject where SubjectName=@subjectName) --获取科目ID
declare @totalCount int --总人数 :那一个班级需要考试这一科目
set @totalCount=(select COUNT(*) from Student where ClassId=@classid)
print @totalcount --14
declare @unpassNum int --不及格人数
set @unpassNum=(select COUNT(distinct Studentno) from Result where SubjectId=@subjectId and StudentNo in(select StudentNo from Student where ClassId=@classid) and StudentResult<60)
while(@unpassNum>@totalCount/2)
begin
--执行循环加分
update Result set StudentResult+=2 where SubjectId=@subjectId and StudentNo in(select StudentNo from Student where ClassId=@classid) and StudentResult<=98
--重新计算不及格人数
set @unpassNum=(select COUNT(distinct Studentno) from Result where SubjectId=@subjectId and StudentNo in(select StudentNo from Student where ClassId=@classid) and StudentResult<60)
end
go
declare @subjectName nvarchar(50)='office' --科目名称
declare @subjectId int--科目ID
declare @classid int =(select classid from Subject where SubjectName=@subjectName) --查询当前科目属于那一个班级
set @subjectId=(select SubjectId from Subject where SubjectName=@subjectName) --获取科目ID
declare @totalCount int --总人数
set @totalCount=(select COUNT(*) from Student where ClassId=@classid)
print @totalcount --14
declare @unpassNum int --不及格人数
while(1=1)
begin
set @unpassNum=(select COUNT(distinct Studentno) from Result where SubjectId=@subjectId and StudentNo in(select StudentNo from Student where ClassId=@classid) and StudentResult<60)
if(@unpassNum>@totalCount/2)
update Result set StudentResult+=2 where SubjectId=@subjectId and StudentNo in(select StudentNo from Student where ClassId=@classid) and StudentResult<=98
else
break
end
?
?
15.子查询
?
子查询--一个查询中包含另外一个查询。被包含的查询就称为子查询,。包含它的查询就称父查询
1.子查询的使用方式:使用()包含子查询
2.子查询分类:
1.独立子查询:子查询可以直接独立运行
查询比“王八”年龄大的学员信息
select * from Student where BornDate<(select BornDate from Student where StudentName='王八')
2.相关子查询:子查询使用了父