2014-10-28 65 views
0

甲骨文更新我有兩個表:與子查詢別名

**PRODUCTS**: 
*PART ITEM* 
M1  A1 
M1  A2 
M1  A3 
M2  B1 
M2  B2 
M3  C1 
M3  C2 
... 

**PARTS**: 
*PART CODE* 
M1  XYZ 
M2  XYZ 
M3  ABC 
A1  XYZ 
A2  MNO 
A3  <null> 
B1  <null> 
B2  <null> 
C1  <null> 
... 

基本上,我想通過
更新部分表中的空 - 以現有的代碼中的一個從PARTS.CODE
- 哪裏PART.PART = PRODUCT.PART
- 上PRODUCT.ITEM

匹配部分。第一部分

我至今是:

update PARTS t2 
set t2.CODE = 
    (
    select tx.CODE, t1.ITEM 
    from PARTS tx 
    join PRODUCTS t1 
     on tx.PART = t1.PART 
) a 
where t2.PART = a.ITEM 
    and t2.CODE is null 

內部選擇調出我需要的ITEM和CODE - 至少它看起來像它會匹配下面的'where'。我得到的錯誤是:
錯誤:ORA-00933:SQL命令不能正確地結束

謝謝了...

回答

0
update PARTS t2 
set t2.CODE = (select max(tx.CODE) 
       from PARTS tx join PRODUCTS t1 on tx.PART = t1.part 
       where t2.part = t1.item 
      ) 
where t2.CODE is null 
    and exists(select 1 from PRODUCTS t1 where t1.item = t2.part and t2.part = t1.part); 
+0

謝謝,我得到0行更新,但有目前有8個_nulls_,所以我需要搗亂一下。 – user1628169 2014-10-28 21:07:34

+0

@ user1628169是的,你的情況看起來很奇怪:「where PARTS.PART = PRODUCT.PART and PARTS.PART = PRODUCT.ITEM」 – Multisync 2014-10-28 21:12:18

+0

想想我 - 有幾次工作,需要測試更多。一些細微的變化: 更新零件t2的 集t2.CODE = ( 選擇tx.CODE \t - 選擇MAX(tx.CODE) 從產品T1 \t \t - 翻轉 內加入TX \t \t零件 - - 翻轉 上t1.PART = tx.PART 其中t2.PART = t1.ITEM ) - ,其中t2.CODE爲空 - 和存在 其中存在 ( 從PRODUCTS T1選擇t1.PART where t2.PART = t1.ITEM \t - rmvd「and」line ) – user1628169 2014-10-28 22:21:37