我需要创建此查询中涉及三个表,而且我不确定如何加入这两个表(地址& address_key )为了更新办公室表格。
我有一个办公室表,一个地址表和一个 address_key 表。 办公室表包含地址数据(街道,城市,州,邮政编码)和办公室ID#。
地址表包含地址数据和地址ID#。
address_key 表包含address_id和office_id。我需要使用地址表中的地址数据更新 office 表中的现有行,并将这些行与 address_key 表中的键相关联。
我如何更新 office 表格地址数据字段?
答案 0 :(得分:1)
您可以循环遍历address_key表,对于每一行,使用office_id使用地址信息更新office记录,使用您循环的每一行中的address_id。我认为如果你使用oracle(这个问题用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
我希望有所帮助!
答案 1 :(得分: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
答案 2 :(得分:0)
您可以根据联接运行UPDATE。
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