在JOIN中的子查询后的表名称

时间:2017-08-03 16:13:45

标签: sql oracle

我是oracle子查询的新手,我面对这个子查询:

select  CUSTOMER.CUSTOMER_ID, CUSTOMER.CUSTOMER_RCE_ID, CUSTOMER.COMM_REGNUM_CUST,CUSTOMER.GROUP_CODE, CUSTOMER.CUSTOMER_CORPORATE_NAME, 
Replace(CUSTOMER.ADDRESS_COMPANY, '|', ', ') as ADDRESS_COMPANY, CUSTOMER.COUNTRY_CODE, CUSTOMER.POST_CODE, CUSTOMER.CITY, CUSTOMER.CUSTOMER_ATTRIBUTE1,
CUSTOMER.CUSTOMER_ATTRIBUTE2, CUSTOMER.CUSTOMER_ATTRIBUTE3, CUSTOMER.CUSTOMER_ATTRIBUTE4, CUSTOMER.CUSTOMER_ATTRIBUTE5, 
CUSTOMER.CREATED_DATE, CUSTOMER.CREATED_BY,CUSTOMER.UPDATED_DATE, CUSTOMER.UPDATED_BY, CUSTOMER.TENANT_ID, 
CUSTOMER.CUSTOMER_ACCOUNT, CUSTOMER.VAT_INTRA, CUSTOMER.VAT_SETTLEMENT,CUSTOMER.IS_HEADQUARTERS, CUSTOMER.CUSTOMER_SAP_ID, 
CUSTOMER.ADDRESSGUID, CUSTOMER.SIREN_CORP, CUSTOMER.COMMERCIAL_SEGMENT, CUSTOMER.CODE_NAF, CUSTOMER.JUR_STATUS_COMP, 
CUSTOMER.SHARE_CAP_AMT,CUSTOMER.IS_ACTIVE_RCE, CUSTOMER.REGISTRATION_PLACE,STATUS.STATUS_NAME , 
CUSTOMER.RCE_ETAB_ID, REPLACE (CUSTOMER.COMM_REGNUM_CUST,' ','') AS SIRET  from CUSTOMER

join CUSTOMER_STATUS on CUSTOMER_STATUS.CUSTOMER_STATUS_ID = (
select * from (select CUSTOMER_STATUS_ID from CUSTOMER_STATUS 
where CUSTOMER.CUSTOMER_ID = CUSTOMER_STATUS.CUSTOMER_ID order by CUSTOMER_STATUS.UPDATED_DATE desc)  CUSTOMER_STATUS where rownum = 1)

我不明白这一部分:

select * from (select CUSTOMER_STATUS_ID from CUSTOMER_STATUS 
where CUSTOMER.CUSTOMER_ID = CUSTOMER_STATUS.CUSTOMER_ID order by CUSTOMER_STATUS.UPDATED_DATE desc)  CUSTOMER_STATUS where rownum = 1

如何在子查询后放置CUSTOMER_STATUS?为什么?

提前致谢。

1 个答案:

答案 0 :(得分:0)

这是一个有趣的联接。闭括号后的Customer_Status是表别名。您可以轻松地将其替换为sadfkjhasldkgjh,它可以正常工作。它告诉程序您已在表结构中选择了数据,并且您将在此处通过此名称(别名)引用它。它使您更容易使用。

在这种情况下,整个陈述有点复杂。您显然已经有一个名为CUSTOMER_STATUS的表,并且您将子查询别名为同名。 Oracle会让你这样做,但结果很快就会变得混乱无法跟踪。最佳做法是,不要将子查询别名为现有表的名称。