假设我有一张桌子付款。
ID | Amount | Date | Reference
1 | 10 | 2020-09-01 | 911
然后我有一张表registered_payments。
ID | Amount | Date | Note | Bank Reference
1 | 10 | 2020-09-01 | 911aa | 000001
2 | 10 | 2020-09-05 | 123vv | 000002
3 | 10 | 2020-09-07 | 555ss | 000003
然后我们有表bank_payments。
ID | Amount | Date | Bank Reference | Bank ID
1 | 10 | 2020-09-01 | 000001 | 44
2 | 10 | 2020-09-05 | 000002 | 33
3 | 10 | 2020-09-07 | 000003 | 22
然后我有一个查询:
WITH cte AS(
SELECT amount,
date,
reference
FROM
payments
WHERE
ID = 1
)
SELECT
*
FROM
registered_payments
WHERE
amount = (SELECT amount FROM tmp)
AND date = (SELECT date FROM tmp)
AND note ilike '%' || (SELECT amount FROM tmp) || '%';
我需要显示此查询的结果(不是问题)。
但是我可以在另一个查询中从结果(始终为1条记录)中使用银行参考,并以某种方式在bank_payments中搜索付款吗?
我只有阅读权限。无法创建任何函数或表。使用了PSQL。
感谢您的帮助。
答案 0 :(得分:0)
如果我正确地遵循了您的说明,您可以将逻辑简化为简单的联接:
select ... -- enumerate the columns you want here
from payments p
inner join registered_payments rp
on rp.amount = p.amount
and rp.date = p.date
and rp.note ilike '%' || p.reference || '%'
inner join bank_payments bp
on bp.amount = rp.amount
and bp.date = rp.date
and bp.bank_reference = rp.bank_reference