• 表连接


    表连接的分类有:1相等

    ①     内连接

    ②     外连接 :左连接。

             右连接。

    交叉连接(左连接加上右连接的结果)。

                       

                    2:不相等。

                    3:自连接。

     

    -------------------------------------------------------相等于连接------------------------------

    用例1

    Select teacher_id,name,department_name from Teachers,Departments

     Where teachers.department_id=Departments.department_id;

    用例2

     Select s.id,Studentname,gradename from Student s, grade g where

     s.gradeid=g.gradeid

    用例3:

    Select s.studentid,c.course_id,course_name, from Couses c, Students_grade g where c.course_id=g.couseid;’

     

    -------------------------------------------------------交叉外连接----------------------------------------

    用例1

    Select A.id,B.id from A left join B on A.id=B.id

    用例2

    Select A.id,B.id from A left join B where A.id=B.id(+)

    用例3

    Select A.id,Bid from A left outer join B where A.id=B.id

     

    ---------------------------------------------------------不等连接----------------------------------------------

    ---------------------------------------------------用例1-------------------------------------

    select student_id,score,grade form studnets_grade g,grades g

    where sg.score between g.low_score and g.high_score;

     

    ----------------------------------------------------------------复杂内连接-------------------------------

    用例1

    Select teacher_id,name department_name from Teachers t,Departments d

     Where t.department_id=d.department_id and title=’讲师’

    用例2

    Select s.student_id,name,course_id,score from Student s,students_grade sg

    Where s.student_id=sg.student_id and specialty=”计算机”;

    用例3

    Select c.course_id,course_name,student_id,score from courses c,Students_grade sg

     Where c.course_id=sg.course._id and course_name=”c++语言程序设计”;

    用例4

    Select s.student_id,s.name,count(*) as 所修课程门数

       From Student s, Students_grade sg  Where s.student_id=sg.student_id

     Group by s.student_id,s.name  having count(*)>1

    order by s.student_id;

    用例5(两个以上)表连接

    select  s.name,course_name,score from Students s, Courses c,Studnets_grade sg where s.student_id=sg.student_id and c.course_id=sg.course_id group by s.student_id,s.name,c.course_name;;

    用例6

     Select s.student_id,s.name,c.course/_name,avg(sg.score) as 平均成绩

     From Studnets s,courses c,Studnets_grade sg where s.student_id=sg.student_id and c.couse_name;

       ----内连接sqlservert                                               

    1: select grade.*,student.* from grade inner join student on grade.gradeid= student.gradeid

       ---内连oracle

    2:select grade.*,student.* from grade, student where grade.gradeid= student.gradeid

    ---:左连接oracle

    3:select grade.*,student.* from grade left outer join student where grade.gradeid=student.gradeid

      ---:右连接oracle

    4:select grade.*,student.* from grade right outer join student where grade.gradeid=student.gradeid

       --sqlservert左连

    5:select grade.*,student.* from grade left join student on grade.gradeid= student.gradeid

       --oracle右连接                                                     

    6:select  grade.*,student.* from grade,student where grade.gradeid=student.gradeid(+);

      或者

    select  grade.*,student.* from grade,student where grade.gradeid=student.gradeid(right);

     

     

    --交叉连接

    7:select grade.*,student.* from grade left join student on grade.gradeiid=student.gradeid

    Union

    Select grade.*,student.* from right join student on grade.gradeid=student.gradeid

    --不等于连接

    8: select sg.studnet_id,sg.course_id,g.grade from student t_grade,sg.grades g where sg.score

     Between g.score and g.high

     --自连接

    9:select s1.name,s2.name from students s1.students S2 where s1.nameid=s2.studactid;

     

         

     

                    

  • 相关阅读:
    业余无线电A类考试准备笔记
    关于互联网技术基层绩效管理的一些思考
    适合产品经理的十本书 From 俞军
    从敏捷开发到微服务,maybe再到中台
    Golang内存模型
    CSS中的那点事儿(一)--- CSS中的单位2
    CSS中的那点事儿(一)--- CSS中的单位1
    design.js
    model.js
    云技术相关的概念
  • 原文地址:https://www.cnblogs.com/1-9-9-5/p/7553751.html
Copyright © 2020-2023  润新知