zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    8. String to Integer (atoi)
    PHP Warning: strftime(): It is not safe to rely on the system's timezone set
    Jackson 使用
    用vim去掉utf-8 BOM
    oracle 11g 从 dmp 文件中导出 sql 代码 的方法.
    git gitosis 添加项目
    Linux gcc和gdb程序调试用法 {转}
    Dos中转义符
    HTML样式链接到外部样式表
    转:财富与智慧
  • 原文地址:https://www.cnblogs.com/big-lll/p/6566523.html
Copyright © 2011-2022 走看看