zoukankan      html  css  js  c++  java
  • mysql关联查询和联合查询

    一、内联方式

       1.传统关联查询

         "select * from students,transcript where students.sid=transcript.sid and transcript.total>600 and transcript.date=2015-6";

         上面是查询出在2015-6月,月考中总成绩超过600的学生信息。where的条件有三个,要看出是哪些是关联条件,哪些是查询过滤还是挺简单,若是多个表多个查询条件那么就不是那么容易了

       2.JOIN...ON

          "select * from students JOIN transcript ON(students.sid=transcript.sid) where transcript.total>600 and transcript.date=2015-6";

         这样能很容易看出ON后面括号即关联条件,where之后为过滤条件一目了然。语法中ON后的()不是必须的,但是为了代码风格建议添加。

     3.JOIN...USING

       "select * from students JOIN transcript USING(sid) where transcript.total>600 and transcript.date=2015-6";

        这个时候括号就是必须的了。这种写法很好,输入更少的单词,查询性能上没有太大的差异,但是JOIN...ON和传统关联查询查询时sid会出现二次。

     4.LEFT OUTER JOIN...ON

      "select * from students LEFT OUTER JOIN transcript USING(sid) where transcript.date=2015-6";

      选择表students所有数据,并将transcript的数据加到students表中,若transcript中不符合条件,就不用加入结果表中,并且NULL表示

     5.RIGHT OUTER JOIN...ON

      "select * from students RIGHT OUTER JOIN transcript USING(sid) where transcript.date=2015-6";

      选择表transcript所有数据,并将students的数据加到students表中,若students中不符合条件,就不用加入结果表中,并且NULL表示

     6.FULL OUTER JOIN...ON

      "select * from students FULL OUTER JOIN transcript USING(sid) where transcript.date=2015-6";

      选择表student、transcript所有数据,若students,students中不符合条件,就不用加入结果表中,并且NULL表示

  • 相关阅读:
    贡献15本经典C、C++、MFC、VC++教程,都是pdf完整版的
    雪花
    孙鑫C++视频教程 rmvb格式 全20CD完整版 精品分享
    mac上用VMWare虚拟机装win7
    阿里云如何解析域名,搭建云服务器环境
    2. Windows编程基础
    复制指定目录下的全部文件到另一个目录中
    Select查询命令
    使用OneNote2016发送博客
    Linux数字雨
  • 原文地址:https://www.cnblogs.com/huixuexidezhu/p/4987852.html
Copyright © 2011-2022 走看看