基于邮政编码表更新表

时间:2012-08-22 22:12:28

标签: sql sql-server zip

我正在寻找一种方法来更新包含city, state, zip, and county的表,方法是使用包含所有必要信息的不同表。

简单地说,我有一个表格(我们称之为Table1),它列出cities, states, and counties所有zip code(或者你想把它们放入的任何顺序)。我想使用Table1中的Table2更新另一个表(称之为city, state, and county),特别是zip code

编辑:MS SQL Server

2 个答案:

答案 0 :(得分:4)

UPDATE A
SET A.City = B.City
FROM Table2 AS A
INNER JOIN Table1 B
ON A.PostalCode = B.PostalCode
WHERE A.City IS NULL --Or Some Other Criteria

答案 1 :(得分:0)

试试这个

UPDATE Table2
SET t2.City = t1.City,
t2.state= t1.state,
t2.county= t1.county

FROM Table2 AS t2
LEFT JOIN Table1 t1
ON t2.zipcode= t1.zipcode