如何基于许多子查询的结果集更新表的多个记录

时间:2013-04-03 18:34:29

标签: sql postgresql

我必须更新表“合约”的10行和3个字段

每10行必须使用不同的值进行更新。

   update  contract_equipment_purchase   set date_shipped = (
    select lat.tstamp from lease_asset la join CR983_TMP  tmp on la.serial_num=tmp.serial  join 
         (select lease_asset_id ,max(tstamp) as tstamp from lease_asset_transaction where status='Placed' group by lease_asset_id)
             lat on la.id=lat.lease_asset_id join lease_asset_status las on las.id=la.id and las.status<>'Out of Service' ),
lease_asset_id= (
        select la.id from lease_asset la join CR983_TMP  tmp on la.serial_num=tmp.serial  join 
             lease_asset_status las on las.id=la.id and las.status<>'Out of Service' ),
comprehensive_expiration_date='2013-10-31'
     where id_contract in (
        select cep.id_contract from contract_equipment_purchase cep join contract c on cep.id_contract =c.id_contract and c.id_contract_type=10 
            join facilities f on f.id=c.id_facility
            join CR983_TMP crtmp on crtmp.account=f.accounting_id);

我面临的主要问题是所有子查询都给出了多个结果,因为我必须使用不同的值更新多个行。 我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

这种语法可以为您提供所需的内容

update contract
set contract.account = s.account,
shipitem = i.item,
date='2013-3-25'
from contract c
  inner join shippertable s on c.account = s.account
  inner join itemtable i on c.item = i.item
where contractname = (select name from customers where)