从值中提取字符串并插入到字段中

Extract string from value and insert into field

这是我的table:

+----+-------------+  
| id | data        |  
+----+-------------+  
|  1 | DA-1111 A   |  
|  2 | DA-5334 B   |  
|  3 | DA-4532 A   |  
|  4 | DA-34 K     |  
+----+-------------+  

有查询:

select substring_index(substring_index(myfield, '-', -1), ' ', 1) as colB from mytable

我得到了 DA- 和最后一个字母之间的提取值。现在我想从字段中提取值,同时将该值插入到同一 table 中的新字段中。最后的结果应该是:

+----+-------------+------+  
| id | data        | colB |
+----+-------------+------+  
|  1 | DA-1111 A   | 1111 |  
|  2 | DA-5334 B   | 5334 | 
|  3 | DA-4532 A   | 4532 | 
|  4 | DA-34 K     | 34   | 
+----+-------------+------+

这可能吗?如何实现?

你可以这样做:

update mytable
set colB = substring_index(substring_index(myfield, '-', -1), ' ', 1)

当然 table 应该已经有一个名为 colB 的字段,否则您可以使用 ALTER TABLE 语句创建它。