如何在 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;
输出
+---------+ | ZipCode | +---------+ | 9030 | | 3902 | | 9083 | | 9089 | +---------+ 4 rows in set (0.00 sec)
以下是搜索和替换字符串开头的字符的查询。这里,我们只处理以 90 开始的邮政编码的记录 -
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)
广告