![](/img/trans.png)
[英]how to copy column from one table data to another table and insert more columns same time in mysql?
[英]How To Populate One Column Based On Another Columns Data In Same Table?
我將兩個數據集合並到一個表中。 一個是美國地址,另一個是加拿大地址。 我需要能夠按國家/地區排序的選項。 因此,我在表格中插入了一個空白的國家/地區列,現在的表格如下所示:
state | country | zip
-----------------------
| |
| |
我有州和郵政編碼數據,但“國家/地區”列為空。 由於美國郵政編碼格式為5位數字(例如12345),而CAN郵政編碼的格式類似(例如G5Y 5S7),因此mysql是否可以使用某種形式填充國家/地區數據...
UPDATE table
SET country = US
WHERE zip (LIKE or =) (US zip format);
然后返回並使用某種WHERE zip LIKE NULL
來用CAN填充其他國家/地區數據?
謝謝
您可以在zip
列中檢查值的長度。
UPDATE table
set country = 'US'
where char_length(zip) = 5;
然后:
UPDATE table
set country = 'CAN'
where country is null
-或一口氣-
UPDATE table t
set country =
(case when char_length(t.zip) = 5
then 'US'
else 'CAN'
end);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.