如何使用MySQL UPDATE删除连字符?

如何使用MySQL UPDATE删除连字符?

使用MySQL update来删除连字符,你可以使用replace()函数。语法如下:

update yourTableName
   set yourColumnName=replace(yourColumnName,'-', '' );

为了理解上述语法,让我们创建一个表。创建表的查询如下所示-

mysql> create table removeHyphensDemo
   -> (
   -> userId varchar(100)
   -> );
Query OK, 0 rows affected (0.62 sec)

使用insert命令在表中插入一些记录。查询如下所示-

mysql> insert into removeHyphensDemo values('John-123-456');
Query OK, 1 row affected (0.22 sec)
mysql> insert into removeHyphensDemo values('Carol-9999-7777-66555');
Query OK, 1 row affected (0.19 sec)
mysql> insert into removeHyphensDemo values('123456-Bob-8765');
Query OK, 1 row affected (0.14 sec)
mysql> insert into removeHyphensDemo values('1678-9870-Sam');
Query OK, 1 row affected (0.21 sec)

使用select语句显示表中的所有记录。查询如下所示-

mysql> select *from removeHyphensDemo;

以下是输出结果-

+-----------------------+
| userId                |
+-----------------------+
| John-123-456          |
| Carol-9999-7777-66555 |
| 123456-Bob-8765       |
| 1678-9870-Sam         |
+-----------------------+
4 rows in set (0.00 sec)

以下是删除连字符的查询-

mysql> update removeHyphensDemo
   -> set userId=replace(userId,'-','');
Query OK, 4 rows affected (0.26 sec)
Rows matched: 4 Changed: 4 Warnings: 0

让我们再次检查表记录。查询如下所示-

mysql> select *from removeHyphensDemo;

以下是没有连字符的输出结果-

+--------------------+
| userId             |
+--------------------+
| John123456         |
| Carol9999777766555 |
| 123456Bob8765      |
| 16789870Sam        |
+--------------------+
4 rows in set (0.00 sec)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程