SQl Server 表链接查询

之前漏下了,这里补一偏

select * from student,score ——笛卡尔积

    可以想想成c#里面的多维函数的样子,打印时每一张表的每一条数据都会连带着第二张表的所有数据

两个表的连接:

第一种方法(比较常用)

        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

原文地址:https://www.cnblogs.com/big-lll/p/6566523.html