MySQL 搜索是否多个字符串包含特殊字符?\n
要搜索字符串是否包含特殊字符,可以使用 REGEXP。以下是语法规范 −
select * from yourTableName where yourColumnName REGEXP '[^a-zA-Z0-9]';
我们首先创建一个表 −
mysql> create table specialCharactersDemo -> ( -> StudentId varchar(100) -> ); Query OK, 0 rows affected (0.58 sec)
使用 insert 命令在表中插入记录。以下是查询 −
mysql> insert into specialCharactersDemo values('STU_1234'); Query OK, 1 row affected (0.15 sec) mysql> insert into specialCharactersDemo values('STU567'); Query OK, 1 row affected (0.14 sec) mysql> insert into specialCharactersDemo values('STU#1234'); Query OK, 1 row affected (0.13 sec) mysql> insert into specialCharactersDemo values('STU897$'); Query OK, 1 row affected (0.18 sec) mysql> insert into specialCharactersDemo values('STU999'); Query OK, 1 row affected (0.43 sec) mysql> insert into specialCharactersDemo values('STU1010'); Query OK, 1 row affected (0.14 sec
以下是使用 select 语句从表中显示所有记录的查询 −
mysql> select *from specialCharactersDemo;
这将生成以下输出 −
+-----------+ | StudentId | +-----------+ | STU_1234 | | STU567 | | STU#1234 | | STU897$ | | STU999 | | STU1010 | +-----------+ 6 rows in set (0.00 sec)
以下是搜索字符串是否包含特殊字符的查询 −
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)
广告