2
我有一個包含一些不正確數據的表,我想更正這些表。對於前 - 在'address_detail'表中,地址字段錯誤地以'null'作爲前綴而不是城市名稱。根據與外鍵的連接更新列中的數據
id | city_id | address
-----------------------------
7 | 1 | null/Street39
8 | 2 | null/Street40
9 | 3 | null/Street41
10 | 4 | null/Street42
city table
id | name
---------------
1 | Mexico
2 | New York
3 | Washington
4 | Los Angeles
修正後,我希望數據如:
address_detail表
id | city_id | address
-------------------------------
7 | 1 | Mexico/Street39
8 | 2 | New York/Street40
9 | 3 | Washington/Street41
10 | 4 | Los Angeles/Street42
我嘗試下面的腳本,但給我的錯誤。
UPDATE
address_detail ad
SET
ad.address = (CONCAT(SELECT name FROM city c WHERE c.id = ad.city_id, SUBSTRING(ad.address, 5)))
WHERE
ad.address LIKE 'null/%'
請您提供了錯誤 – Gunaseelan