zoukankan      html  css  js  c++  java
  • join的使用



    我们还是以这四张表为例:
    1.left join
    select * from student a left join score b on a.sid=b.sid;

    也就是说A中的元素都会显示,没有值的用Null填充,结果如下:

    2.right join

    select * from student a right join score b on a.sid=b.sid;

     也就是说只会显示B中存在的元素,结果如下

     3.outer join

     

     MySQL不支持OUTER JOIN,但是我们可以对左连接和右连接的结果做UNION操作来实现。

    select * from student a left join score b on a.sid=b.sid union select * from student a right join score b on a.sid=b.sid;

     

     4.inner join

    内连接INNER JOIN是最常用的连接操作。从数学的角度讲就是求两个表的交集,从笛卡尔积的角度讲就是从笛卡尔积中挑出ON子句条件成立的记录。有INNER JOIN,WHERE(等值连接),STRAIGHT_JOIN,JOIN(省略INNER)四种写法。

     select * from student a inner join score b on a.sid=b.sid;

     select * from student a join score b on a.sid=b.sid;

     select * from student a, score b where a.sid=b.sid;

     select * from student a STRAIGHT_JOIN  score b on a.sid=b.sid;

     

     5.using子句

    MySQL中连接SQL语句中,ON子句的语法格式为:table1.column_name = table2.column_name。当模式设计对联接表的列采用了相同的命名样式时,就可以使用 USING 语法来简化 ON 语法,格式为:USING(column_name)。 所以,USING的功能相当于ON,区别在于USING指定一个属性名用于连接两个表,而ON指定一个条件。另外,SELECT *时,USING会去除USING指定的列,而ON不会。实例如下。

     select * from student a inner join score b using(sid);

     6.nature join

    自然连接就是USING子句的简化版,它找出两个表中相同的列作为连接条件进行连接。有左自然连接,右自然连接和普通自然连接之分。

    select * from student natural join score;

  • 相关阅读:
    浅谈命令混淆
    为你解惑之Silverlight经典10问详解 (转载)
    Prism 简介
    Prism学习笔记(二)简单的MVVM模式
    Prism学习笔记(一) 从Hello World开始
    修改Oracle数据库序列
    将身份证号转换为年龄
    获取文件类型
    下划线转驼峰
    驼峰转下划线
  • 原文地址:https://www.cnblogs.com/xiximayou/p/11648625.html
Copyright © 2011-2022 走看看