0

我想做这样的事情:

update t1 set t1.column1 = 'changed'
from left outer join t2 on t1.dataid = t2.dataid
where t2.column2 != 'foo';

t2基本上,在决定要更新哪些t1记录时,我需要检查一些东西。我怎样才能使它在语法上正确?

4

2 回答 2

1

Try something like:

update t1 
set t1.column1 = 'changed'
from t2
where t1.dataid = t2.dataid
  and t2.column2 != 'foo';

If you really need outer join try something like:

update t1
set t1.column1 = 'changed'
from (select 1 as dummy) dummy_table
left join t2 on t1.dataid = t2.dataid
            and t2.column2 != 'foo';

Havent tested - it may not work.

Another possible way is to do a inner join to a "copy" of t1 and then do a left join to t2

于 2013-10-30T16:26:42.743 回答
1

与执行 LEFT JOIN 相比,在 WHERE 子句中建立与另一个表的关联更简单,因为关联表中的值不必传输到更新的表。

它看起来像这样:

update t1 set t1.column1 = 'changed'
where not exists (select 1 from t2 where t1.dataid = t2.dataid and t2.column2='foo');
于 2013-10-31T11:33:36.883 回答