2014-10-28 73 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