-2

我需要创建此查询中涉及的三个表,并且我不确定如何连接这两个(地址和地址)表以更新办公室表。

我有一张办公室表、一个地址表和一个地址表。办公室表有地址数据(街道、城市、州、邮编)和办公室 ID#。

地址表具有地址数据和地址Id#。

address_key表有address_id 和office_id。我需要使用地址表中的地址数据更新 office 表中的现有行,并将它们address_key 表中的键链接起来

我将如何更新办公室表地址数据字段?

4

3 回答 3

1

You could loop through the address_key table and for each row, update the office record with office_id with the address info using the address_id in each row you're looping through. I think something like this should work if you're using oracle (the question was tagged with oracle11g):

FOR id_row in (SELECT * FROM address_key)
LOOP
    UPDATE office SET (street, city, state, zip) = 
        (SELECT street, city, state, zip FROM address WHERE address_id = id_row.address_id);
    WHERE office_id = id_row.office_id
END LOOP

I hope that helps!

于 2013-03-07T17:52:09.143 回答
0
update o set
    street = a.street
    , city = a.city
    , state = a.state
    , zip = a.zip
from Office o
join Address_Key k
    on k.officeid = o.officeID
join Address a
    on a.addressID = k.addressid
于 2013-03-07T17:38:13.287 回答
0

您可以根据您的联接运行更新。

UPDATE office
SET office.street = address.street ,
 office.city = address.city ,
 office.state = address. ,
 office.zip = address.zip 
FROM office
 INNER JOIN address_key ON office.office_id = address_key.office_id
 INNER JOIN address ON address_key.address_id = address.address_id
于 2013-03-07T17:38:27.667 回答