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