我试图通过JOIN从table_2添加新记录到table_1,但我没有得到正确的结果。
我试图复制纬度和经度从表_2到表_1。
我有table_1与列地址,纬度,经度
where latitude &经度为空
table_2有address, latitude, longitude, name, address等列
在这两个表中address只有一个城市名。
insert into table_1 (latitude, longitude)
select table_2.latitude, table_2.longitude from table_2 JOIN table_1 ON table_1.address = table_2.address;
Getting this output: table_1
address latitude longitude
city_1 NULL NULL
city_2 NULL NULL
city_3 NULL NULL
NULL 123.12 123.12
NULL 123.12 123.12
NULL 123.12 123.12
我期待这样的事情:
address latitude longitude
city_1 123.12 123.12
city_2 123.12 123.12
city_3 123.12 123.12
在您的示例中,您应该使用update
语句而不是insert
,因为第一个表中的条目已经存在,您只需要更新它们
update table_1 set latitude = mapped_t2.latitude, longitude = mapped_t2.longitude
from (select table_2.address, table_2.latitude, table_2.longitude from table_2 join table_1 on table_1.address = table_2.address) as mapped_t2
where mapped_t2.address = table_1.address
dbfiddle