我有一个看起来像这样的数据库
leads
name zip city county state
Bill 84058
Susan 90001
FullUSZipCodes
ZipCode City County State
84058 Orem Utah Utah
90001 Los Angeles South California
正如您所看到的,在潜在客户数据库中,城市,计数和州是空的,我试图合并数据,以便第一个表看起来像这样:
leads
name zip city county state
Bill 84058 Orem Utah Utah
Susan 90001 Los Angeles South California
这是我尝试的第一个查询:
UPDATE leads SET leads.county = FullUSZipCodes.County WHERE leads.zip = FullUSZipCodes.ZipCode
哪个不起作用,这是第二个查询:
UPDATE leads INNER JOIN FullUSZipCodes ON leads.zip = FullUSZipCodes.ZipCode SET leads.county = FullUSZipCodes.County, leads.city = FullUSZipCodes.City, leads.state = FullUSZipCodes.State
答案 0 :(得分:1)
你的第二个查询看起来应该可行,但我会稍微改写一下:
UPDATE leads t1
INNER JOIN FullUSZipCodes t2
ON t1.zip = t2.ZipCode
SET
t1.city = t2.City,
t1.county = t2.County,
t1.state = t2.State
WHERE
t1.city IS NULL OR t1.county IS NULL OR t1.state IS NULL;
如果leads
表有很多记录,并且性能是一个问题,您可以尝试在更新查询中添加WHERE
子句,该子句仅针对缺少一个或多个部分的记录地址信息。我认为始终将所有三个列一起更新没有任何问题,因为通常这些信息会聚集在一起。