With our oracle Database/queries that are currently running i have come across some SQL where they have done a table by table join. Now I want to be able to understand this so could someone explain? I am a newbie to this.
SELECT *
FROM ra_customer_trx_all
WHERE customer_trx_id IN
(SELECT customer_trx_id
FROM AR_PAYMENT_SCHEDULES_ALL
WHERE payment_schedule_ID IN
(SELECT payment_schedule_ID
FROM AR_RECEIVABLE_APPLICATIONS_ALL
WHERE applied_customer_trx_id =
SELECT customer_trx_id FROM ra_customer_trx_all WHERE trx_number = '34054'));
答案 0 :(得分:1)
第一: 从表ra_customer_trx_all中选择所有TRX记录,其中number = 34054 我们正在寻找customer_trx_id
select * from ra_customer_trx_all t4 where t4.trx_number = '34054'
第二:选择来自payment_schedule表的所有记录,其中包含来自step1的ID
select * from AR_RECEIVABLE_APPLICATIONS_ALL t3 where t3.payment_schedule_ID = (prev select)
3rd:从customer_trx_all表中选择具有step2
中ID的所有记录select * from AR_PAYMENT_SCHEDULES_ALL t2 where t3.customer_trx_id = (prev select)
第四
select * from ra_customer_trx_all t1 where t2.customer_trx_id = (prev select)
5: 摘要:
如果 trx 是转换 逻辑是:
选择所有已安排通过RECEIVABLE_APPLICATIONS支付的客户交易记录,交易编号为34054
SELECT t1.*
FROM ra_customer_trx_all t1
inner join AR_PAYMENT_SCHEDULES_ALL t2 on t2.customer_trx_id = t1.customer_trx_id
inner join AR_RECEIVABLE_APPLICATIONS_ALL t3 on t3.payment_schedule_ID = t2.payment_schedule_ID
inner join ra_customer_trx_all t4 on t4.customer_trx_id = t3.applied_customer_trx_id
where t4.trx_number = '34054'
答案 1 :(得分:0)
您可以替换
select *
from tableA
where columnA in (select columnB
from tableB
where columnB1 in (select ...))
与
select *
from tableA, tableB
where tableA.columnA = tableB.columnB
and tableB.columnB1 in (select ...)
将此模式按顺序应用于每个子查询
简短说明:在IN
关键字后打开外括号,将表从内部FROM
子句移到外部,并将条件添加到WHERE
子句:IN
之前的列必须相等到子查询中的SELECT
子句中的列。