如何在MySQL列中替换特定的字符?

如何在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)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程