SQL server 數據庫——表連接(多表橫向連接,縱向連接)


                        表連接

1、select * from student,score ——笛卡爾積

2、兩個表的連接:

法1:select student.sno, sname, degree

        from student,score  ----當查詢的列名兩個表中都有時要在列名前面加上‘表名.’

        where student.sno=score.sno

法2:select cno, (select sname from student where student.sno=score.sno),degree from score

法3:select student.sno ,sname,cno,degree

       from student join score     --- //inner join(默認) //left join(以左表為主表)  //right join(以右表為主表)

       on student.sno=score.sno

3、三個表的連接

法1:

       select student.sno, sname, cno, degree, name     ---注:select student.sno, sname, cno, degree+10, name+‘同學’ 結果是:成績增加10分,名字后面加上同學

       from student,score,course  ----當查詢的列名不只存在1個表中都有時要在列名前面加上‘表名.’

       where student.sno=score.sno

       and   score.cno=course.cno

法2:select student.sno, sname, cno, degree, name

       from student join score

      on student.sno=course.sno

          join course

     on score.cno=course.cno

4、縱鏈接

      


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM