如果右侧table没有数据,加入tables显示null

Join tables to display null if right side table doesnt have data

我有一个feedback_ques,feedback_anstable如下:

feedback_ques
 
id  question                created_date    created_by delete_date
1   How was the training    16-SEP-20       900         null
2   facility?               16-SEP-20       900         null    
3   Dept?                   16-SEP-20       902         null
4   Infrastructure          16-SEP-20       900         16-SEP-20

feedback_ans

ques_id member_id   answers created_date    created_by
1       10          good    16-SEP-20       891
2       10          good    16-SEP-20       891
3       10          poor    16-SEP-20       891
4       10          good    16-SEP-20       891

我想加入 table 如下:

select q.id as id, a.member_id as memberId, q.question as Ques, a.answers as Ans
from feedback_ques q, feedback_ans a
where q.id = a.ques_id(+) 
and a.member_id = 10
and q.DELETE_DATE IS NULL;

这给了我所有的字段。如果在答案 table 中找不到答案,我希望查询 return 为 null。 例如 member_id 20 没有答案,所以我希望 table 显示此查询的空值,如下所示。

select q.id as id, a.member_id as memberId, q.question as Ques, a.answers as Ans
from feedback_ques q, feedback_ans a
where q.id = a.ques_id(+) 
and a.member_id = 20
and q.DELETE_DATE IS NULL;

ID  memberId    Ques                    ans
1   20          How was the training    null
2   20          facility?               null
3   20          Dept?                   null
4   20          Infrastructure          null

更新: 按照我的建议,我使用 leftOuter join 如下:

select q.id as id, a.member_id as memberId, q.question as Ques, a.answers as Ans
from feedback_ques q
left join feedback_ans a on a.ques_id = q.id and a.member_id = 20
and q.delete_date is null;

但是当 delete_date 为 !=null 时,此查询不起作用。该行仍由查询 return 编辑。从上面的任务 4 不应该 returned 因为 delete_date != null。 你能帮忙吗

这些old-shool,隐式连接,让人难以表达你想要的。这里where子句中的条件a.member_id = 20过滤掉不匹配的行

这只是应始终使用显式标准联接的原因之一。考虑一个left join,其中左边table的所有条件都放在on子句中:

select q.id as id, a.member_id as memberId, q.question as Ques, a.answers as Ans
from feedback_ques q
left join feedback_ans a on a.ques_id = q.id and a.member_id = 20
where q.delete_date is null;