我有一个oracle sql,我在accounts表和payment表之间进行外连接。我想知道如何在付款表中有匹配的记录时执行条件p.payment_status_code ='R'。其余的,不应该执行条件。
FROM
accounts a, payments p
WHERE (a.account_balance <= a.low_balance_level OR a.account_balance <= 0)
AND a.account_id = p.account_id(+)
AND p.payment_status_code = 'R'
此致 -Anand
答案 0 :(得分:1)
FROM
accounts a, payments p
WHERE (a.account_balance <= a.low_balance_level OR a.account_balance <= 0)
AND a.account_id = p.account_id(+)
AND p.payment_status_code(+) = 'R'
^^^
答案 1 :(得分:1)
如果我理解正确,您只想用payment_status_code = 'R'
显示结果,对吧?在那种情况下,为什么你需要使用外连接?你可以使用传统的连接。你想做什么?
FROM
accounts a, payments p
WHERE (a.account_balance <= a.low_balance_level OR a.account_balance <= 0)
AND a.account_id = p.account_id
AND p.payment_status_code = 'R'
如果您想显示帐户信息,即使是没有付款的用户,也可以使用左外连接而不是右外连接
FROM
accounts a
left join payments p on a.account_id = p.account_id AND p.payment_status_code = 'R'
WHERE (a.account_balance <= a.low_balance_level OR a.account_balance <= 0)
答案 2 :(得分:0)
FROM
accounts a, payments p
WHERE (a.account_balance <= a.low_balance_level OR a.account_balance <= 0)
AND a.account_id = p.account_id(+)
AND (p.account_id is null or p.payment_status_code = 'R')