SQL Server将表中的2列合并为另一个表中的1列

时间:2019-05-09 16:16:32

标签: sql-server

我有2个表,这些表包含基于位置的数据。

表1具有地名,县,纬度和经度(4列)。

表2具有地名,县和LatLng(三列)。

我想从表1中提取纬度和经度数据,并用逗号将它们分开,然后插入表2中的PlaceName和County匹配的LatLng列中。

2 个答案:

答案 0 :(得分:0)

我认为简单的JOINconcat就能解决问题

Update A
   set LatLng = concat(B.Latitude,',',B.Longitude)
 From  Table2 A
 Join  Table1 B 
   on  A.PlaceName=B.PlaceName and A.Country = B.Country

答案 1 :(得分:0)

您需要连接表以进行UPDATE:

UPDATE t2
SET t2.LatLng = t1.Latitude + ',' + t1.Longtitude 
FROM Table2 AS t2 INNER Table1 AS t1
ON t2.PlaceName = t1.PlaceName AND t2.County = t1.County