Old SQL to the New. table by table joins

时间:2015-11-12 16:12:34

标签: oracle

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'));

2 个答案:

答案 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子句中的列。