如何在 MySQL 列中替換特定字元?


要替換特定字元,請使用 REPLACE(),要更新,請使用 UPDATE 命令。讓我們首先建立一張表 -

mysql> create table DemoTable1899
   (
   Code varchar(20)
   );
Query OK, 0 rows affected (0.00 sec)

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

mysql> insert into DemoTable1899 values('John_123');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1899 values('32189_Sam_987');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1899 values('Miller_David_456_909');
Query OK, 1 row affected (0.00 sec)

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

mysql> select * from DemoTable1899;

這會產生以下輸出 -

+----------------------+
| Code                 |
+----------------------+
| John_123             |
| 32189_Sam_987        |
| Miller_David_456_909 |
+----------------------+
3 rows in set (0.00 sec)

這是替換特定字元的查詢 -

mysql> update DemoTable1899 set Code=replace(Code,'_','');
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3 Warnings: 0

讓我們再次檢查表記錄

mysql> select * from DemoTable1899;

這會產生以下輸出 -

+-------------------+
| Code              |
+-------------------+
| John123           |
| 32189Sam987       |
| MillerDavid456909 |
+-------------------+
3 rows in set (0.00 sec)

更新於: 2019-12-27

308 次瀏覽

開啟你的 職業生涯

完成課程並獲得認證

入門
廣告
© . All rights reserved.