具有不匹配条件的更新语句

时间:2017-03-11 10:55:53

标签: sql oracle sql-update

我有一个更新查询,如下所示 -

udate table1 tbl_out
set column1 =
    (select colValue from table1 t1, table2 t2
    where table1.product_id = table2.product_id
    and t1.id = tbl_out.id)
where tbl_out.column1 is null;

这很有效。但是有些列的table1.product_id值为null。 因此,对于这些记录where table1.product_id = table2.product_id条件不满足且记录未更新。

我尝试按以下方式添加此条件 -

where (table1.product_id = table2.product_id OR table1.product_id is null)
and t1.id = tbl_out.id

但是这将返回多于1行,并且上面的update语句将失败。 我知道在这种情况下出现了一些可能非常简单的问题,但可能并不清楚。

Ca有人帮我这个吗?

2 个答案:

答案 0 :(得分:0)

   udate table1 tbl_out
 set column1 =
(select colValue from table1 t1, table2 t2
where nvl(table1.product_id,table2.product_id) = table2.product_id
and t1.id = tbl_out.id)
where tbl_out.column1 is null;

答案 1 :(得分:0)

我认为您正在寻找一个简单的相关子查询,这意味着您不需要子查询中的<div class="container"> <div class="left"> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</p> <p>Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.</p> <p>Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi.</p> <p>Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,</p> </div> <div class="right"> <p>Yeah</p> </div> </div>

JOIN

您可能还需要update table1 tbl_out set column1 = (select t2.colValue from table2 t2 where t2.product_id = tbl_out.product_id ) where tbl_out.column1 is null; 子句:

exists

编辑:

如果update table1 tbl_out set column1 = (select t2.colValue from table2 t2 where t2.product_id = tbl_out.product_id ) where tbl_out.column1 is null and exists (select 1 from table2 t2 where t2.product_id = tbl_out.product_id ); 中的值为NULL,并且您尝试将其视为有效密钥:

table2