当前位置:Gxlcms > 数据库问题 > SQl Server 表链接查询

SQl Server 表链接查询

时间:2021-07-01 10:21:17 帮助过:16人阅读

两个表的连接:

第一种方法(比较常用)

        select student.sno, sname, degree

        from student,score  ----当查询的列名两个表中都有时要说明查的是那个表中的列 用   表明点列名(student.sno

        where student.sno=score.sno  -- 连个表共有的那一列,这样就可以说明两个表建立联系

第二种方法

        select cno, (select sname from student where student.sno=score.sno),degree from score  套路是一样的,位置不相同

第三种方法

       select student.sno ,sname,cno,degree from student join score on student.sno=score.sno

               --- //inner join(默认) //left join(以左表为主表)  //right join(以右表为主表)

   

三个表的连接

第一种方法(跟上面两个表一样)

       select student.sno, sname, cno, degree, name ---如果degree+10, name+‘同学’ 结果是:成绩数值增加10名字后面加上同学,数值类型就会计算,字符串类就会拼接

       from student,score,course  ----当查询的列名不只存在1个表中都有时要用   表明点列名(student.sno)说明是那一个表中的

       where student.sno=score.sno

       and   score.cno=course.cno

第二种方法

       select student.sno, sname, cno, degree, name

       from student join score

       on student.sno=course.sno

          join course

     on score.cno=course.cno

纵链接    数据类型要一致

两个表之间用 union

 举个栗子:

 select tname 名字,tsex 性别,tbirthday 生日 from teacher
 union
 select sname ,ssex,sbirthday from student

 

SQl Server 表链接查询

标签:nbsp   连接   left join   color   sel   比较   值类型   c#   server   

人气教程排行