How to use oracle outer join with a filter where clause

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 15:01:46

问题


If i write a sql:

select * 
from a,b 
where     a.id=b.id(+) 
      and b.val="test"

and i want all records from a where corresponding record in b does not exist or it exists with val="test", is this the correct query?


回答1:


You're much better off using the ANSI syntax

SELECT *
  FROM a
       LEFT OUTER JOIN b ON( a.id = b.id and
                             b.val = 'test' )

You can do the same thing using Oracle's syntax as well but it gets a bit hinkey

SELECT *
  FROM a, 
       b
 WHERE a.id = b.id(+)
   AND b.val(+) = 'test'

Note that in both cases, I'm ignoring the c table since you don't specify a join condition. And I'm assuming that you don't really want to join A to B and then generate a Cartesian product with C.




回答2:


Move the condition into the JOIN clause and use the ANSI standard join pattern.

SELECT NameYourFields,...
FROM A
LEFT OUTER JOIN B
ON A.ID = B.ID
AND B.VAL = 'test'
INNER JOIN C
ON ...



回答3:


A LEFT OUTER JOIN is one of the JOIN operations that allow you to specify a join clause. It preserves the unmatched rows from the first (left) table, joining them with a NULL row in the shape of the second (right) table.

So you can do as follows :

SELECT
FROM a LEFT OUTER JOIN b ON a.id = b.id

--Note that you have used double quote "test" which is not used for varchar in SQL you should use single quote 'test'

AND b.val = 'test';




回答4:


SELECT * FROM abc a, xyz b
 WHERE a.id = b.id
   AND b.val = 'test'


来源:https://stackoverflow.com/questions/18390588/how-to-use-oracle-outer-join-with-a-filter-where-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!