要搜索字符串是否包含特殊字符,可以使用REGEXP。以下是语法-
select * from yourTableName where yourColumnName REGEXP '[^a-zA-Z0-9]';
让我们首先创建一个表-
mysql> create table specialCharactersDemo -> ( -> StudentId varchar(100) -> );
使用insert命令在表中插入记录。以下是查询-
mysql> insert into specialCharactersDemo values('STU_1234'); mysql> insert into specialCharactersDemo values('STU567'); mysql> insert into specialCharactersDemo values('STU#1234'); mysql> insert into specialCharactersDemo values('STU897$'); mysql> insert into specialCharactersDemo values('STU999'); mysql> insert into specialCharactersDemo values('STU1010');
这是用于查询字符串是否包含特殊字符的查询-
mysql> select *from specialCharactersDemo -> where StudentId REGEXP '[^a-zA-Z0-9]';
这将产生以下输出-
+-----------+ | StudentId | +-----------+ | STU_1234 | | STU#1234 | | STU897$ | +-----------+ 3 rows in set (0.02 sec)
您可以对以上结果使用其他语法。以下是查询-
mysql> select *from specialCharactersDemo -> where StudentId REGEXP'[^[:alnum:]]';
这将产生以下输出-
+-----------+ | StudentId | +-----------+ | STU_1234 | | STU#1234 | | STU897$ | +-----------+ 3 rows in set (0.05 sec)