Mysql基础知识点汇总

2019-01-04 20:53:14王旭

  end

14.WHILE循环语法

1.没有{},使用begin..end
2.没有bool值,需要使用条件表达式
3.可以嵌套
4.也可以使用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.子查询分类:

独立子查询:子查询可以直接独立运行.
查询比“王八”年龄大的学员信息
select * from Student where BornDate<(select BornDate from Student where StudentName='王八')
相关子查询:子查询使用了父查询中的结果
--子查询的三种使用方式
--1.子查询做为条件,子查询接在关系运算符后面  >  < >= <= = <> !=,如果是接这关系运算符后面,必须保证 子查询只返回一个值
--查询六期班的学员信息