要使用MySQL更新删除连字符,可以使用replace()
函数。语法如下-
update yourTableName set yourColumnName=replace(yourColumnName,'-', '' );
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table removeHyphensDemo -> ( -> userId varchar(100) -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into removeHyphensDemo values('John-123-456'); mysql> insert into removeHyphensDemo values('Carol-9999-7777-66555'); mysql> insert into removeHyphensDemo values('123456-Bob-8765'); mysql> insert into removeHyphensDemo values('1678-9870-Sam');
使用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,'-',''); Rows matched: 4 Changed: 4 Warnings: 0
让我们再次检查表记录。查询如下-
mysql> select *from removeHyphensDemo;
这是不带连字符的输出-
+--------------------+ | userId | +--------------------+ | John123456 | | Carol9999777766555 | | 123456Bob8765 | | 16789870Sam | +--------------------+ 4 rows in set (0.00 sec)