简体   繁体   中英

Left join with where clause for right table (Must return NULL from right) - Oracle

I have 2 tables ID and Comm. The tables are as follows

ID                          
AppID   Name
1       James
2       John
.
.
100     Jeff

Comm
AppID  Comment
1      abc
1      def
1      pqr
2      abc
2      def
2      pqr
3      def

I want all appID from ID (First table) and from Comm(Second table) I want only those comment which are equal to abc , rest others should be NULL.

I am using following query, not sure how do I filter comment abc and Null

select id.appid,comm.comment
from id left join comm on
id.appid=comm.appid
where comm.comment = 'abc'

I know I have the logic wrong, trying to figure out where should I change. Any help is appreciated.

select id.appid,comm.comment
from id 
left join comm 
    on id.appid=comm.appid
    and comm.comment = 'abc'

Use two left join, and then union them.

select id.appid,comm.comment 
from id left join comm 
on id.appid=comm.appid 
where comm.comment = 'abc'
union
select comm.appid,comm.comment 
from comm  left join id
on idcomm.appid=id.appid
where comm.comment = 'abc'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM