如何替換 MySQL 表中的字元?


要僅替換單個字元,請在 MySQL 中使用 REPLACE()。我們首先建立一個表 -

mysql> create table DemoTable
   -> (
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (0.69 sec)

使用 insert 命令在表中插入一些記錄 -

mysql> insert into DemoTable values('John Smitk');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('David Miller');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('Adam Smitk');
Query OK, 1 row affected (0.11 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select *from DemoTable;

這將產生以下輸出 -

+--------------+
| Name         |
+--------------+
| John Smitk   |
| David Miller |
| Adam Smitk   |
+--------------+
3 rows in set (0.00 sec)

以下是替換字元的查詢 -

mysql> update DemoTable
   -> set Name=replace(Name,'k','h');
Query OK, 2 rows affected (0.13 sec)
Rows matched: 3 Changed: 2 Warnings: 0

讓我們再次檢查表記錄 -

mysql> select *from DemoTable;

這將產生以下輸出 -

+--------------+
| Name         |
+--------------+
| John Smith   |
| David Miller |
| Adam Smith   |
+--------------+
3 rows in set (0.00 sec)

更新於: 17-12-2019

321 次瀏覽

開啟您的職業

完成課程獲得認證

開始
廣告
© . All rights reserved.