如何在 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 次浏览

开启你的 职业生涯

完成课程,获得认证

开始
广告