SQL--多表查询

连接查询的where子句中用来连接两个表的条件称为连接条件,用于连接的两个属性值必须可以比较。

              格式:表名1.列名  比较运算符  表名2.列名  

连接的几种方式:等值连接和非等值连接,自身连接,外连接,复合条件连接

             eg:  //复合连接

                             表名1.列名  比较运算符  表名2.列名   and  表名1.列名  比较运算符  表名2.列名  

嵌套查询 :  带有IN谓词的子查询;带有比较运算符的子查询;带有ANY,ALL谓词的子查询;

                带有Exists谓词的子查询(相关自查询);

相关子查询的处理过程是:首先取外层查询表中的第一个元组,根据它与内层查询相关的属性值   处理内层查询,

                                        若where返回值为真值,择取外层查询中 的该元组的数据存入结果表。

                                        for(){

                                                    for(){

                                                          }

           集合查询:select查询是元组的集合,多个select结果的集合可以进行集合操作:union , intersect, except.  

                                                                                                                               并集  ,交集,  差集

           select  Sname 

           From    Student

           where  Sno  IN

                               (  select  Sno

                                   from   SC

                                    where Cno='2');

           select  Sname ,Sage

           From    Student

           where  Sage <ANY(    select  Sage

                                              from   Student

                                               where  Sdept='CS')     AND    Sdept<>'CS'

            //查询其他系中,比计算机科学系某一学生小的学生姓名和年龄


              查询 选修了全部课程的学生姓名。

                 学生---->学号----->所有 的课程号

             select Sname

              From Student              

             where Not Exists(

                            select *

                             from  Course

                              where  not exist(

                                         selcet *

                                          from SC

                                          where  Sc.Cno=Course.Cno

                                                      and     Sc.Sno=Student.Sno

                                                          ));



猜你喜欢

转载自blog.csdn.net/zenglingcheng/article/details/80544974