[pgsql-jp: 32294] Re: 他のテーブルのデータにて主テーブルデータを更新

Akira Sakima akira @ lequio.ne.jp
2004年 2月 19日 (木) 19:03:45 JST


崎間です。

すでに中川さんから回答がありますので
別解として・・・・。

> 【SQL文】
> update table_a 
> set address = (select address1 from table_b )
> where id = (select id1 from table_b);
>
> update table_a 
> set comment = (select comment1 from table_b )
> where id = (select id1 from table_b);

このままだとエラーになるので少し直して

例1)
update table_a
set address = (select address1
from table_b where table_a.id = table_b.id1)
where id in (select id1 from table_b);

update table_a
set comment = (select comment1
from table_b where table_a.id = table_b.id1)
where id in (select id1 from table_b);


又は一つにまとめて

例2)
update table_a
set address = b.address1,
comment = b.comment1
from table_b b
where id = b.id1


Oracleのマニュアル(Ver7.2当時ですが(^_^;))では
例1のような更新を「相関更新」と説明されていますが
その用語で検索しても、あまりヒットしないようです。
一般的にはなんて呼ぶのでしょうね?

-- 
Akira Sakima <akira @ lequio.ne.jp>




pgsql-jp メーリングリストの案内