如何在 MySQL 表格值中向任何空格新增特定字元?


為此,請使用 REPLACE() 函式並用字元替換空格。讓我們首先建立一個表格 -

mysql> create table DemoTable (Subject text);
Query OK, 0 rows affected (0.86 sec)

示例

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

mysql> insert into DemoTable values('Introduction to MySQL');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Java in depth');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values('Data Structure and Algorithm');
Query OK, 1 row affected (0.16 sec)

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

mysql> select *from DemoTable;

輸出

+------------------------------+
| Subject |
+------------------------------+
| Introduction to MySQL |
| Java in depth |
| Data Structure and Algorithm |
+------------------------------+
3 rows in set (0.00 sec)

以下是替換空格的查詢 -

mysql> SELECT *,REPLACE(Subject,' ','_') as Subject_Name from DemoTable;

輸出

+-------------------------------+------------------------------+
| Subject | Subject_Name |
+-------------------------------+------------------------------+
| Introduction to MySQL | Introduction_to_MySQL |
| Java in depth | Java_in_depth |
| Data Structure and Algorithm | Data_Structure_and_Algorithm |
+-------------------------------+------------------------------+
3 rows in set (0.00 sec)

更新於:09-Mar-2020

338 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.