【2017-3-13】SQL Server 表连接

select * from Student

select * from Course
select * from Score
select * from Teacher

 

基础的表连接    --把多个表的多列显示在一个结果集里面 where之后的内容是连接条件
select * from Student,Score where Student.Sno = Score.Sno

 

 

 

两个表连接    --注意:要查询的列如果有重复的,要标明是哪个表的这一列
select Student.Sno,Sname,Cno,Degree from Student,Score where Student.Sno = Score.Sno

另一种  jion on       --inner join / left join --以左边的表为主 / right join --以右边的表为主
select Student.Sno,Sname,Cno,Degree from Student join Score on Student.Sno = Score.Sno

另一种 子查询方法        --返回的Sname是调用的Student表中的Sname来映射到结果集,所以 无列名
select Sno,(select Sname from Student where Student.Sno = Score.Sno),Cno,Degree from Score

 

 

多表连接      --必须有一个表来进行中转,做中介
select Student.Sno,Sname,Score.Cno,Degree,Cname
from Student,Score,Course
where Student.Sno = Score.Sno
and Score.Cno = Course.Cno

 


多表纵连接      --注意:多个表的列必须对应,连接后的列名是第一个表的列名
select Sno,Sname,Ssex,Sbirthday from Student
union
select Tno,Tname,Tsex,Tbirthday from Teacher

转载于:https://www.cnblogs.com/hanqi0216/p/6545317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值