简体   繁体   English

左内或左连接

[英]Inner or Outer left Join

I'm having difficulty modifying a script for this situation and wondering if someone maybe able to help: 我在针对这种情况修改脚本时遇到困难,并且想知道是否有人可以提供帮助:

I have an address table and a phone table both sharing the same column called id_number . 我有一个address表和一个phone表,它们共享同一个称为id_number列。 So id_number = 2 on both tables refers to the same entity. 因此,两个表上的id_number = 2都指向同一实体。 Address and phone information used to be stored in one table (the address table) but it is now split into address and phone tables since we moved to Oracle 11g. 地址和电话信息曾经存储在一个表( address表)中,但是由于我们移至Oracle 11g,因此现在分为addressphone表。

There is a 3rd table called both_ids . 第3个表称为both_ids This table also has an id_number column in addition to an other_ids column storing SSN and some other ids. 该表还具有id_number除了一列other_ids列存储SSN和其他一些IDS。

Before the table was split into address and phone tables, I had this script: (Written in Sybase) 在将该表分为address表和phone表之前,我有以下脚本:(用Sybase写)

INSERT INTO sometable_3 (          
SELECT a.id_number, a.other_id, 
       NVL(a1.addr_type_code,0)      home_addr_type_code,             
       NVL(a1.addr_status_code,0)    home_addr_status_code,             
       NVL(a1.addr_pref_ind,0)      home_addr_pref_ind,             
       NVL(a1.street1,0)             home_street1,             
       NVL(a1.street2,0)              home_street2,             
       NVL(a1.street3,0)              home_street3,             
       NVL(a1.city,0)                 home_city,             
       NVL(a1.state_code,0)           home_state_code,             
       NVL(a1.zipcode,0)              home_zipcode,             
       NVL(a1.zip_suffix,0)           home_zip_suffix,             
       NVL(a1.telephone_status_code,0) home_phone_status,             
       NVL(a1.area_code,0)             home_area_code,             
       NVL(a1.telephone_number,0)      home_phone_number,             
       NVL(a1.extension,0)             home_phone_extension,             
       NVL(a1.date_modified,'')      home_date_modified               
 FROM both_ids a, address a1          
 WHERE a.id_number = a1.id_number(+) 
 AND a1.addr_type_code = 'H');   

Now that we moved to Oracle 11g, the address and phone information are split. 现在,我们移至Oracle 11g,地址和电话信息已拆分。

How can I modify the above script to generate the same result in Oracle 11g? 如何在Oracle 11g中修改以上脚本以生成相同的结果?

Do I have to first do INNER JOIN between address and phone tables and then do a LEFT OUTER JOIN to both_ids? 我必须先在地址和电话表之间进行INNER JOIN,然后再对both_ids进行LEFT OUTER JOIN吗?

I tried the following and it did not work: 我尝试了以下操作,但没有成功:

Insert Into..
  select ...
    FROM a1. address
      INNER JOIN t.Phone ON a1.id_number = t.id_number
      LEFT OUTER JOIN both_ids a ON a.id_number = a1.id_number
    WHERE a1.adrr_type_code = 'H'

There is a syntax error in your query. 您的查询中存在语法错误。 You wrote in comment below that your query is: 您在下面的评论中写道,您的查询是:

 ..
 FROM address a1 JOIN telephone t ON a1.id_number = t.id_number
 AND RIGHT OUTER JOIN tt_gl_both_ids a ON a.id_number = a1.id_number
 WHERE
 ..

Throw out AND and it will work. 扔掉AND ,它将起作用。 Also, replace right outer join with left outer join, since that's what you're trying to achieve (again, based on comments you wrote). 另外,将右外部联接替换为左外部联接,因为这就是您要实现的目标(同样,基于您编写的注释)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM