SQL server 数据库——表连接(多表横向连接,纵向连接)

时间:2023-03-08 23:25:12
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、纵链接