如何在 MySQL 中搜索和替换字符串开头的特定字符?
mysqlmysqli database更新于 2025/7/5 8:44:17
为此,您可以使用 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)
以下是搜索和替换字符串开头字符的查询。这里,我们仅处理 ZipCode 以 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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

