如何使用 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)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP