I'm trying to update the table api_hotspot's existing rows according to the data I've exported from another table on a different database. For each tuple in the export I want to set the fields address=tuple[1], LAT=tuple[2], LNG=tuple[3] on the row who's id=tuple[0], but I'm not sure if this is the correct way of doing it.

UPDATE `api_hotspot` SET `LAT`=[2],`LNG`=[3],`address`=[1] WHERE id = [0] VALUES
(9, '272 College St S #9, Monmouth, OR 97361, USA', 44.846668, -123.239334),
(11, '169 Main St W, Monmouth, OR 97361, USA', 44.848419, -123.236961),
(12, '1103 Goucher St, Amity, OR 97101, USA', 45.111313, -123.199585),
(13, '380 Pacific Ave N, Monmouth, OR 97361, USA', 44.851665, -123.229607),
(14, '2410 NW Burnside Ct. Gresham, OR, 97030', 0.000000, 0.000000),
...

How can I do this without using regex to replace each line manually?

UPDATE `api_hotspot` SET `LAT`=44.846668,`LNG`=-123.239334,`address`='272 College St S #9, Monmouth, OR 97361, USA' WHERE id = 9
有帮助吗?

解决方案

Read the data into a table e.g. export (id, address, lat, lng) and do an update with a join

UPDATE api_hotspot
LEFT JOIN export ON export.id = api_hotspot.id
SET api_hotspot.address = export.address, api_hotspot.lat = export.lat, api_hotspot.lng = export.lng
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top