如何将特定字符添加到 MySQL 表值中的任何空白处?

mysqlmysqli database

为此,使用 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)

相关文章