zoukankan      html  css  js  c++  java
  • MySQL数据库之多表查询left join左外连接

    左外连接

    • 规则

      • 以左边的表为准,右边如果没有对应的记录用null显示
    • 语法

      • select * from 表1 left join 表2 on 表1.公共字段=表2.公共字段
    MariaDB [sel]> select math,id from grades left join resume on grades.name=resume.name;
    +------+------+
    | math | id   |
    +------+------+
    |   96 |    1 |
    |   91 |    3 |
    |   94 | NULL |
    |   94 | NULL |
    +------+------+
    # `4 rows in set (0.001 sec)`
    
    MariaDB [sel]> select math,id from resume left join grades on grades.name=resume.name;
    +------+----+
    | math | id |
    +------+----+
    |   96 |  1 |
    |   91 |  3 |
    | NULL |  2 |
    +------+----+
    # `3 rows in set (0.001 sec)`
    
    • 思考:
      • select * from 表1 left join 表2 on 表1.公共字段=表2.公共字段select * from 表2 left join 表1 on 表1.公共字段=表2.公共字段 一样吗?
      • 答:不一样,第一个SQL以表1为准,第二个SQL以表2为准。
  • 相关阅读:
    vim 去掉自动注释和自动回车
    性别回归
    表情识别
    python list按字典的key值排序
    pytorch学习率策略
    python将list元素转为数字
    php面向对象
    mysql
    mysql
    mysql
  • 原文地址:https://www.cnblogs.com/SharkJiao/p/14137832.html
Copyright © 2011-2022 走看看