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   |
    +------+------+--------+------+--------+
  • 相关阅读:
    Python学习-字符编码浅析
    python元组,集合类型,及字典补充
    python字符串,列表常用操作
    python流程控制
    Python之线程&进程
    yii框架的中的一些使用介绍
    《最牛B的Linux Shell命令》笔记
    CentOS6.8上安装epel
    mysql 5.7 Group Replication
    mysql5.7 参数记录 (持续更新)
  • 原文地址:https://www.cnblogs.com/jingdenghuakai/p/11648496.html
Copyright © 2011-2022 走看看