zoukankan      html  css  js  c++  java
  • SQL语句的四种连接

    SQL的四种连接查询

      内连接

        inner join 或者 join

      外连接

        左连接   left join 或者 left outer join

        右连接  right join 或者 right outer join

        完全外连接  full join 或者 full outer join

     先创建数据库testjoin

     create database testjoin;

    然后引用此数据库testjoin

     use testjoin;

    然后创建person表和card表

     create table person(id int,name varchar(20),cardid int);
     create table card(id int,name varchar(20));

    然后在表中插入数据

    insert into card values (1,'饭卡'),(2,'建行卡'),(3,'农行卡'),(4,'工商卡'),(5,'邮政卡');
     insert into person values (1,'张三',1);
     insert into person values (2,'李四',3);
     insert into person values (3,'王五',6);

    +------+--------+
    | id | name |
    +------+--------+
    | 1 | 饭卡 |
    | 2 | 建行卡 |
    | 3 | 农行卡 |
    | 4 | 工商卡 |
    | 5 | 邮政卡 |
    +------+--------+
    5 rows in set (0.00 sec)                

    +------+------+--------+
    | id | name | cardid |
    +------+------+--------+
    | 1 | 张三 | 1 |
    | 2 | 李四 | 3 |
    | 3 | 王五 | 6 |
    +------+------+--------+
    3 rows in set (0.00 sec)

    两张表并没有设置外键

    1.inner join 查询(内连接查询)

      内连接查询就是两张表中的数据,通过某个相等的字段进行查询,查询出的结果是两张表都有的数据,即查询两张表的交集

    mysql> select * from person inner join card on person.cardid=card.id;
    +------+------+--------+------+--------+
    | id   | name | cardid | id   | name   |
    +------+------+--------+------+--------+
    |    1 | 张三 |      1 |    1 | 饭卡   |
    |    2 | 李四 |      3 |    3 | 农行卡 |
    +------+------+--------+------+--------+

    2.left join 查询(左外连接)

      左外连接会把左面表中的数据全部取出来,而右边表中的数据,如果有相等的就像是出来,如果没有就补充NULL

    mysql> select * from person left  join card on person.cardid=card.id;
    +------+------+--------+------+--------+
    | id   | name | cardid | id   | name   |
    +------+------+--------+------+--------+
    |    1 | 张三 |      1 |    1 | 饭卡   |
    |    2 | 李四 |      3 |    3 | 农行卡 |
    |    3 | 王五 |      6 | NULL | NULL   |
    +------+------+--------+------+--------+

    3.right join 查询(右外连接)

      右外连接会把右面表中的数据全部取出来,而左边表中的数据,如果有相等的就像是出来,如果没有就补充NULL

    mysql> select * from person right  join card on person.cardid=card.id;
    +------+------+--------+------+--------+
    | id   | name | cardid | id   | name   |
    +------+------+--------+------+--------+
    |    1 | 张三 |      1 |    1 | 饭卡   |
    |    2 | 李四 |      3 |    3 | 农行卡 |
    | NULL | NULL |   NULL |    2 | 建行卡 |
    | NULL | NULL |   NULL |    4 | 工商卡 |
    | NULL | NULL |   NULL |    5 | 邮政卡 |
    +------+------+--------+------+--------+

    4.mysql 不支持full join

    mysql> select * from person full  join card on person.cardid=card.id;
    ERROR 1054 (42S22): Unknown column 'person.cardid' in 'on clause'

    mysql要想实现全连接,可以使用左外连接和右外连接的并集union进行连接

    mysql> select * from person right  join card on person.cardid=card.id
     union 
    select * from person left join card on person.cardid=card.id;
    +------+------+--------+------+--------+
    | id   | name | cardid | id   | name   |
    +------+------+--------+------+--------+
    |    1 | 张三 |      1 |    1 | 饭卡   |
    |    2 | 李四 |      3 |    3 | 农行卡 |
    | NULL | NULL |   NULL |    2 | 建行卡 |
    | NULL | NULL |   NULL |    4 | 工商卡 |
    | NULL | NULL |   NULL |    5 | 邮政卡 |
    |    3 | 王五 |      6 | NULL | NULL   |
    +------+------+--------+------+--------+
  • 相关阅读:
    C#单例模式的三种写法转载
    silverlight 添加配置项
    oracle 如何实现上一条、下一条、查找不连续的值
    一个IT民工眼中的保障房不能保证公平,赞成取消保障房
    c# where 转载
    进度条 silverlight
    中国软件公司我深表认同:软硬结合
    计算经纬度两点之间的距离(c#)
    如何高效使用SQLITE .NET (C#)
    如何判断系统是否安装了flash插件
  • 原文地址:https://www.cnblogs.com/jingdenghuakai/p/11648496.html
Copyright © 2011-2022 走看看