zoukankan      html  css  js  c++  java
  • left join 中 on 与 where 理解

    mysql> select * from a_company;
    +----+---------+------------+
    | NO | company | code       |
    +----+---------+------------+
    | D  | 2371    | 4000023555 |
    | A  | 2106    | 4000023556 |
    | E  | 2A08    | 5000016000 |
    | B  | 2106    | 2000000012 |
    | C  | 2106    | 3000023555 |
    +----+---------+------------+
    5 rows in set (0.02 sec)
    
    mysql> select * from a_pice;
    +----+-------+------------+--------+
    | id | BWKEY | MATNR      | VERPR  |
    +----+-------+------------+--------+
    |  1 | 2106  | 3000023555 |   2.11 |
    |  2 | 2106  | 5000016003 | 1599.9 |
    |  3 | 2A08  | 5000016000 | 458.27 |
    |  6 | 3769  | 2000000012 | 284.58 |
    +----+-------+------------+--------+
    4 rows in set (0.03 sec)
    
    mysql> select * from a_company c left join a_pice p on c.company=p.bwkey and c.code=p.matnr ORDER BY c.no;
    +----+---------+------------+------+-------+------------+--------+
    | NO | company | code       | id   | BWKEY | MATNR      | VERPR  |
    +----+---------+------------+------+-------+------------+--------+
    | A  | 2106    | 4000023556 | NULL | NULL  | NULL       | NULL   |
    | B  | 2106    | 2000000012 | NULL | NULL  | NULL       | NULL   |
    | C  | 2106    | 3000023555 |    1 | 2106  | 3000023555 |   2.11 |
    | D  | 2371    | 4000023555 | NULL | NULL  | NULL       | NULL   |
    | E  | 2A08    | 5000016000 |    3 | 2A08  | 5000016000 | 458.27 |
    +----+---------+------------+------+-------+------------+--------+
    5 rows in set (0.03 sec)
    
    mysql> select * from a_company c left join a_pice p on c.company=p.bwkey and c.code=p.matnr where c.NO in ('A','C') ORDER BY c.no;
    +----+---------+------------+------+-------+------------+-------+
    | NO | company | code       | id   | BWKEY | MATNR      | VERPR |
    +----+---------+------------+------+-------+------------+-------+
    | A  | 2106    | 4000023556 | NULL | NULL  | NULL       | NULL  |
    | C  | 2106    | 3000023555 |    1 | 2106  | 3000023555 |  2.11 |
    +----+---------+------------+------+-------+------------+-------+
    2 rows in set (0.04 sec)
    
    mysql> select * from a_company c left join a_pice p on c.company=p.bwkey and c.code=p.matnr where p.id>2 ORDER BY c.no;
    +----+---------+------------+----+-------+------------+--------+
    | NO | company | code       | id | BWKEY | MATNR      | VERPR  |
    +----+---------+------------+----+-------+------------+--------+
    | E  | 2A08    | 5000016000 |  3 | 2A08  | 5000016000 | 458.27 |
    +----+---------+------------+----+-------+------------+--------+
    1 row in set (0.06 sec)
    

      

    select * from a LEFT JOIN b on a.id=a.xid and a.name=a.xname and a.id=2 where a.id=3 and b.xname='abc';

    • 先left连接,连接方法是on,再对连接的结果进行where筛选;
    • 连接的结果:主表全部记录,关联表仅符合连接方法的记录,不符合的为空填充。

    select记录中主表部分:不受on影响,只受where影响;(id=2为无效,id=3有效)

    select记录中关联表部分:即受on影响(符合on的记录才进入到连接的结果,不符合的为空填充),也受where影响。

    • 也就是说:主表的筛选条件须在 where 中,on 后面的筛选条件是仅针对于关联表
    Tty725 说:
    欢迎转载,但请注明内容的来源或URL;
    [转]”篇章,必须保留原始来源且勿添加本blog指向。
  • 相关阅读:
    java web 入门实例servlet篇(显示后台数据库列表,删除某一条记录并显示)
    我的成长比价系列:java web开发过程中遇到的错误一:sql语句换行错误
    spring mvc + velocity 搭建实例程序maven版本并且使用的是tomcat容器而不是jetty(step by step)
    spring mvc学习笔记(一)web.xml文件配置的一点重要信息
    与数据库连接的页面增删改查 的easyui实现(主要是前端实现)
    oracle 11g 空表导出
    EMCA和EMCTL的简单用法
    vs2010补丁
    CAS 策略已被 .NET Framework 弃用
    sqlserver2008 链接服务器 2000
  • 原文地址:https://www.cnblogs.com/Tty725/p/10795552.html
Copyright © 2011-2022 走看看