数据库上基本笔记

2017-04-12  本文已影响0人  panda_say

SQl语言的复杂查询与视图

主要内容:

  1. [not]IN
  2. some / all
  3. [not]exists
  4. 聚集函数
  5. group by
  6. having
  7. 视图

子查询:

例子:求学过001又学过002号课程的学生学号?(无关子查询)
Select S# From SC
Where C# = '001' and
            S# in (Select S# From SC Where C# = '002');
例子:没学过李明老师课程的所有同学的姓名?(无关子查询)
Select Sname From Student
Where S# not in (Select S# From SC.Course C,Teacher T
                             Where T.Tname = '李明' and SC.C# = C.C#
                                          and T.T# = C.T#);

非相关子查询与相关子查询
外层查询的变量的作用域包含内层查询。

例子:学过001号课程的同学名字
Select Sname
From Student Stud
Where S# in (Select S#
                      From SC
                       Where S# = Stud.S# and C# = '001');

some/all子查询

例子:找出所有课程多不及格的学生名字(相关子查询)
Select Sname From Student
 Where 60 > all (Select From Score
                          where Score.S# = Sname.S#);
例子:找出张三同学成绩最低的课程号?(相关子查询)
Select C# From SC,Student S
Where Sname = "张三" and S.S# = SC.S# and
            Score <= all (Select Score Form SC 
                                  Where S# = S.S#);

等价与不等价

上一篇 下一篇

猜你喜欢

热点阅读