zoukankan      html  css  js  c++  java
  • 关于在left join的on子句中限制左边表的取值时出现非期望的结果

    使用的SQL大概是这样的:

    select * from A left join B on A.id=B.id and A.id>10; --错误的使用

    我们期望的结果集应该是 A中的id>10,但是实际上A.id>10 这个限制条件并没有起作用。

    应该改成如下的这种形式:

    select * from A left join B on A.id=B.id where A.id>10;--正确的使用

    这是在oracle的官方文档中找到的相关说明:

    left outer join
    The result of a left outer join for table A and B contains all records of the left table A,
    even if the join condition does not match a record in the right table B. For example, if
    you perform a left outer join of employees (left) to departments (right), and if some
    employees are not in a department, then the query returns rows from employees
    with no matches in departments.


    这是在 《Database System Concepts》这本书中找到的相关说明:

    The right outer join is symmetric with the left outer join: It pads tuples
    from the right relation that did not match any from the left relation with nulls and
    adds them to the result of the natural join. In Figure 6.18, tuple (58583, null, null,
    null, null, Califieri, History, 62000), is such a tuple. Thus, all information from the
    right relation is present in the result of the right outer join.

    大致的意思是,left join的结果集一定会包含左边表的所有记录。同理,right join一定会包含右边表的所有记录。

    所以,使用时应该只在on子句中包含关联条件,单独对某个表的限制应该放到where子句中。

    只是不知道,如果在left join的on子句中单独限制右边的表会不会有利于减少中间表的大小。

  • 相关阅读:
    ASCII对照表
    Python学习记录-3-简明Python教程-数据结构
    Python学习记录-2
    Python新手容易遇到的问题
    python学习问题之-编码
    同步与异步的概念(转自http://blog.chinaunix.net/uid-21411227-id-1826898.html)
    Objective-c学习三
    挺有意思的人体时钟代码(转自http://ziren.org/tools/hone-hone-clock.html)
    int ,long , long long类型的范围
    css3选择器使用例子
  • 原文地址:https://www.cnblogs.com/vanwoos/p/5948327.html
Copyright © 2011-2022 走看看