高级查询与分页

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高级查询与分页相关的知识,希望对你有一定的参考价值。

一.简单子查询

问题1:查询学生表中比美羊羊小的学员信息(姓名,地址)

(不用变量,所想即所得)

 

1 select StudentName,Address from Student
2 where Birthday>select Birthday from Student where StudentName=‘美羊羊’)

 

问题2:查询oop课程至少一次考试刚好等于60分的学生信息(姓名,成绩)

1 select StudentName,StudentResult
2 from Student,Result,Subject
3 where Student.StudentNo=Result.StudentNo
4 and Result.SbujectId=Subject.SbujectId
5 and SubjectName=oop
6 and StudentResult=60

使用子查询实现

分析思路:查询结果是什么,就写到Select后,然后根据需要的条件往下写,用到变量,需要跨表访问时,让子查询帮我们完成.

select StudentName,StudentResult from Student,Result
where(Student.StudentNo=Result.StudentNo)
and SubjectId=(select SubjectId from Subject  where SbujectName=oop)
and StudentResult=60

 

子查询.小结

 

简单子查询(嵌套子查询)的执行机制:

 

将子查询的结果作为外层父查询的一个条件。

 

也就意味着先执行子查询,再执行父查询

 

子查询:子查询语句必须用小括号括起来,

 

然后通过比较运算符:>、<,=等连接起来

 

 注意点:.子查询必须用小阔号括起来,子查询先执行出一个结果,然后将该结果作为父查询的一个条件而存在.

 

二.in和Not in子查询

问题三:查询参加‘oop’课程最近一次考试的在读学生名单(学生姓名,学生编号)

select studentname from student where studentno not in
(
   select studentno from result where subjectid in
   (
      select subjectid from subject where subjectname=oop
   )
   and examdate=
   (
      select max(examdate) from result where subjectid in
      (
         select subjectid from subject where  subjectname=oop
      )
   )
)
and gradeid=  
  select gradeid from grade 
  where gradename=S1
)

当返回的数据不是一行,而是多行的时候使用in反之则使用=.

Exists和Not  Exists子查询

 

问题4:检查“oop”课程最近一次考试.

 1 select * from result
 2 order by subjectid ,examdate    
 3 if exists
 4 (
 5 select * from result where subjectid=
 6  (
 7  select subjectid from subject 
 8 where subjectname=oop
 9 )
10 and examdate=
11 (
12 select max(examdate) from result
13 where subjectid=
14 (
15  select subjectid from subject 
16  where subjectname=oop
17  )
18  )
19  and studentresult>=80
20 )    
21 begin
22 update result set studentresult=100
23 where studentresult>98
24 and subjectid=
25  (
26  select subjectid from subject 
27  where subjectname=oop
28 )
29  and examdate=
30 (
31 select max(examdate) from result
32 where subjectid=
33 (
34 select subjectid from subject 
35 where subjectname=oop
36  )
37  )
38  update result set studentresult+=2
39  where studentresult<=98
40  and subjectid=
41 (
42 select subjectid from subject 
43 where subjectname=oop
44  )
45 and examdate=
46  (
47  select max(examdate) from result
48  where subjectid=
49  (
50  select subjectid from subject 
51  where subjectname=oop
52  )
53  )
54       
55 end
56 else 
57 begin
58 update result set studentresult+=5
59 where studentresult<=95
60 and subjectid=
61  (
62 select subjectid from subject 
63 where subjectname=oop
64 )
65  and examdate=
66  (
67 select max(examdate) from result
68 where subjectid=
69  (
70  select subjectid from subject 
71  where subjectname=oop
72  )
73  )
74 update result set studentresult=100
75 where studentresult>95 
76 and subjectid=
77  (
78 select subjectid from subject 
79 where subjectname=oop
80  )
81 and examdate=
82  (
83 select max(examdate) from result
84  where subjectid=
85  (
86  select subjectid from subject 
87 where subjectname=oop
88  )
89  )
90 end
91 select studentno,studentname from student
92 union
93 select gradeid,gradename from grade
94 --product  (id,proname,category)
95 select distinct(gradename) from grade

重要:if exists(子查询) 子查询返回的必须是一个结果集,而不是一个bool值。

结果集(用一个表结构将数据呈现出来,如果没有结果,返回的是一个空表)

子查询的列可以跟单个列名,也可以跟星号,但是不能跟聚合函数,因为聚合函数

返回的值永远是真,因为聚合函数也是结果集的一种,不能作为Exists判定的依据。

相关子查询的执行依赖于外部查询.多数情况下是子查询的WHERE子句中引用了外部查询的表.执行过程:

(1)从外层查询中取出一个元组,将元组相关列的值传给内层查询.

(2)执行内层查询,得到子查询操作的值.

(3)外查询根据子查询返回的结果或结果集得到满足条件的行.

(4)然后外层查询取出下一个元组重复做步骤1-3,直到外层的元组全部处理完毕.

分页

目的:为了加快网站对数据的查询(检索)速度,我们引入了分页的概念.

1:核心思想:跳过几条取几条(双top 双order by 方式)

双top双order by 例(跳五行查两行)

select top 2* from Student
where StudentNo not in
(select top 5 StudentNo from Student
order by StudentNo)
order by StudentNo

 

 

2:局限性(SQL Server2005之后的版本支持该写法,因为我们要用到row_number() over()函数,在之前是没有该函数)

 

select * from 
(select *,row_number() over(order by studentno) as myid from student) as temp
where myid between 4 and 6

 

以上是关于高级查询与分页的主要内容,如果未能解决你的问题,请参考以下文章

mybatis动态sql片段与分页,排序,传参的使用

分组查询与分页查询

架构师成长记_第八周_13_dsl搜索-查询所有内容与分页查询

架构师成长记_第八周_13_dsl搜索-查询所有内容与分页查询

架构师成长记_第八周_13_dsl搜索-查询所有内容与分页查询

是否可以将FirestorRecyclerAdaper与分页结合起来?