MySQL 如何搜索并替换字符串开头的特定字符
为此,您可以使用INSERT()。 让我们首先创建一个表 –
mysql> create table DemoTable
-> (
-> ZipCode varchar(200)
-> );
Query OK, 0 rows affected (0.47 sec)
使用insert命令在表中插入一些记录 –
mysql> insert into DemoTable values('9030');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('3902');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('9083');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values('9089');
Query OK, 1 row affected (0.13 sec)
使用select语句显示来自表的所有记录 –
mysql> select *from DemoTable;
阅读更多:MySQL 教程
输出
+---------+
| ZipCode |
+---------+
| 9030 |
| 3902 |
| 9083 |
| 9089 |
+---------+
4 rows in set (0.00 sec)
以下是在字符串开头搜索和替换字符的查询。这里,我们仅对以90开头的ZipCode记录进行操作 –
mysql> update DemoTable set ZipCode=INSERT(ZipCode, 1, 2, 'Country-AUS-')
-> where ZipCode LIKE '90%';
Query OK, 3 rows affected (0.26 sec)
Rows matched: 3 Changed: 3 Warnings: 0
让我们再次检查表记录 –
mysql> select *from DemoTable;
输出
+----------------+
| ZipCode |
+----------------+
| Country-AUS-30 |
| 3902 |
| Country-AUS-83 |
| Country-AUS-89 |
+----------------+
4 rows in set (0.00 sec)