![](/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.