如何从 MySQL 数据库字段中移除特殊字符?
你可以使用 REPLACE() 函数从数据库字段中删除特殊字符。特殊字符包括双引号 (“ “)、井号 (#)、美元符号 ($)、百分号 (%) 等。
从数据库字段中删除特殊字符的语法如下。
UPDATE yourTableName SET yourColumnName=REPLACE(yourColumnName,’yourSpecialCharacters’,’’);
为了理解上述语法,我们创建一张表。创建表的查询如下
mysql> create table RemoveSpecialCharacterDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(20), -> PRIMARY Key(Id) -> ); Query OK, 0 rows affected (0.59 sec)
使用插入命令在表中插入一些记录。查询如下
mysql> insert into RemoveSpecialCharacterDemo(Name) values('$John'); Query OK, 1 row affected (0.29 sec) mysql> insert into RemoveSpecialCharacterDemo(Name) values('$Carol'); Query OK, 1 row affected (0.16 sec) mysql> insert into RemoveSpecialCharacterDemo(Name) values('$Mike'); Query OK, 1 row affected (0.17 sec) mysql> insert into RemoveSpecialCharacterDemo(Name) values('$Sam'); Query OK, 1 row affected (0.14 sec) mysql> insert into RemoveSpecialCharacterDemo(Name) values('$Dav$id$'); Query OK, 1 row affected (0.17 sec) mysql> insert into RemoveSpecialCharacterDemo(Name) values('Robert$'); Query OK, 1 row affected (0.30 sec) mysql> insert into RemoveSpecialCharacterDemo(Name) values('J$ames$'); Query OK, 1 row affected (0.13 sec) mysql> insert into RemoveSpecialCharacterDemo(Name) values('Max$well$'); Query OK, 1 row affected (0.27 sec)
使用 select 语句从表中显示所有记录。查询如下
mysql> select *from RemoveSpecialCharacterDemo;
输出如下
+----+-----------+ | Id | Name | +----+-----------+ | 1 | $John | | 2 | $Carol | | 3 | $Mike | | 4 | $Sam | | 5 | $Dav$id$ | | 6 | Robert$ | | 7 | J$ames$ | | 8 | Max$well$ | +----+-----------+ 8 rows in set (0.00 sec)
以下是从数据库字段中删除特殊字符的查询,使用 REPLACE()。
mysql> update RemoveSpecialCharacterDemo -> set Name=replace(Name,'$',''); Query OK, 8 rows affected (0.22 sec) Rows matched: 8 Changed: 8 Warnings: 0
再次检查表记录。显示所有记录的查询如下
mysql> select *from RemoveSpecialCharacterDemo;
输出如下
+----+---------+ | Id | Name | +----+---------+ | 1 | John | | 2 | Carol | | 3 | Mike | | 4 | Sam | | 5 | David | | 6 | Robert | | 7 | James | | 8 | Maxwell | +----+---------+ 8 rows in set (0.00 sec)
查看输出实例,特殊字符 $ 已从表中完全删除。
广告