如何在 MySQL 字串中更改特定字元?
要更改 MySQL 字串中的特定字元,你可以將 CONCAT() 與 SUBSTRING() 結合使用。
讓我們先建立一個表 −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value varchar(200) ); Query OK, 0 rows affected (0.60 sec)
使用 insert 命令向表中插入一些記錄 −
mysql> insert into DemoTable(Value) values('98764'); Query OK, 1 row affected (0.17 sec)
以下是使用 select 語句顯示錶中所有記錄的查詢 −
mysql> select *from DemoTable;
這會產生以下輸出 −
+----+-------+ | Id | Value | +----+-------+ | 1 | 98764 | +----+-------+ 1 row in set (0.00 sec)
以下是更改 MySQL 字串中特定字元的查詢。我們在此設定 X,其中放置了 6 −
mysql> select concat(substring(Value,1,3),'X',substring(Value,5)) from DemoTable;
這會產生以下輸出 −
+-----------------------------------------------------+ | concat(substring(Value,1,3),'X',substring(Value,5)) | +-----------------------------------------------------+ | 987X4 | +-----------------------------------------------------+ 1 row in set (0.00 sec)
廣告