使用的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子句中單獨限制右邊的表會不會有利於減少中間表的大小。