如何在 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)
廣告