优化MySchool数据库(四)

关于“无法附件数据库”过程的遇到的问题:

         1、数据文件本身,具有访问权限的限制

                     ---- 选中 数据库文件所在的文件夹---->右键菜单(属性)----> 安全 --->User用户组(赋予完整控制权限)

         2、数据库版本兼容性问题

                     ---- 数据库分离无效情况下,将整个数据库(结构+数据),导出成SQL

                            选中正在运行的数据库 ---->  右键菜单(任务--生成脚本) --->“高级”设置中---->将“要编写的脚本类型”设置为“架构和数据”

 

所谓“子查询”:

      ---- 将“查询语句”嵌入的其他的SQL语句中。

      ---- 原来由“变量”来担当的责任,您都可以考虑使用查询直接替换

---赋值语句

   declare @age int

   set @age = (子查询)

   print @age

   --- 逻辑语句中

   declare @i int

   set @i= 18

   if @i < (子查询 )

       print '成功提交!'

   else

       print '失败'

   ---其他的Select语句中

select * from [Admin]

    where len(LoginPwd) < (子查询)

 

【注意】

        子查询除了自己独立实现查询得到结果外,也可以使用父查询相关数据

 

“多表联查”中的注意事项:

         1、主表,应该是所有表单中,相互关联性最多的那张表(与其他表有主外键关系最多的那张表)

         2、在inner join的连接顺序上,遵循“连接传递原则”,将要连接的表,一定要与已经inner join连接的表,具有直接主外键关系

 

“多表联查”与“子查询”互替换性:

          1、所有的“多表联查”都可以被“子查询”替换

          2、但不是所有的“子查询”都可以被“多表联查”替换

          3、适用场景:

                      当Select的数据来自于“多张表单”时,适合使用“多表联查”

                      当Where的条件来自于“多张表单”时,适合使用“子查询”

 

“子查询”与“父查询”的连接符号:

 >、>=、<、<=、=、!=

子查询:只能返回一个结果(一行一列)

in

子查询:返回多个结果(多行一列)

Exists

子查询:可以返回任意结果

 

"not in"应用与“分页查询”:

--- 查询整张表,并按照每页10条分割,取出第4页的数据

declare @pageRowCount int ,@page int

set @pageRowCount =10 --每页的记录数

set @page = 2 --你要的页码

 

select top (@pageRowCount) * from temptable-

where id not in (select top ((@page-1) * @pageRowCount) id from TempTable)

 

 

 

 

第四章子查询(最后一个示例)

 

 

 

select 应到人数 =(select count(1) from Student),

          实到人数= (select count(1) from Result

where Result.SubjectNo = (select SubjectNo from Subject where SubjectName = 'java')

  and  Result.ExamDate = (

select max(ExamDate) from Result

where SubjectNo = (select SubjectNo from Subject where SubjectName = 'java')

)

   ),

  缺考人数 = (select count(1) from Student) -

                  (select count(1) from Result

where Result.SubjectNo = (select SubjectNo from Subject where SubjectName = 'java')

  and  Result.ExamDate = (

select max(ExamDate) from Result

where SubjectNo = (select SubjectNo from Subject where SubjectName = 'java')

)

   )

 

-----------------------------------------------------------------------------------------------------------                           

--创建“表变量”,做数据的临时保存

declare @tb table(姓名 varchar(50),学号 int,成绩 varchar(10),是否通过 varchar(10))

 

insert into @tb

select Student.StudentName,

         Student.StudentNo,

 case

when A.StudentResult is null then '缺考'

else cast(A.StudentResult as varchar(10))

         end,

 case

                  when A.StudentResult>=60 then '是'

  else '否'

               end

from Student

left join 

(

select * from Result

where Result.SubjectId = (

select SubjectId from Subject where SubjectName = 'java'

) --java

and    Result.ExamDate= (

select max(ExamDate) from Result where Result.SubjectId =

(select SubjectId from Subject where SubjectName = 'java')

 )  --最后一次(java)

) as A  on Student.StudentNo = A.StudentNo

 

 

 

---从临时表中进行数据查询

select * from @tb

 

--统计结果

select 总人数    = (select count(1) from @tb),

         通过人数 = (select count(1) from @tb where 是否通过='是'),

 通过率 = cast( ((select count(1) from @tb where 是否通过='是') /cast( (select count(1) from @tb) as float)) * 100 as varchar(20)) + '%'

                  

 

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/why168888/p/4278903.html