MySQL 查询仅检索带有特殊字符的列值?
mysqlmysqli database
为此,请使用 REGEXP。让我们首先创建一个表 −
mysql> create table DemoTable(SubjectCode varchar(100)); Query OK, 0 rows affected (0.89 sec)
使用 insert 命令在表中插入一些记录。记录由文本、数字和特殊字符组成 −
mysql> insert into DemoTable values('Java899@22'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('C#'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('~Python232'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('MongoDB%'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('C123456'); Query OK, 1 row affected (0.37 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-------------+ | SubjectCode | +-------------+ | Java899@22 | | C# | | ~Python232 | | MongoDB% | | C123456 | +-------------+ 5 rows in set (0.00 sec)
以下是获取所有带有特殊字符的值的查询 −
mysql> select *from DemoTable where SubjectCode regexp '[^a-zA-Z0-9\&]';
这将产生以下输出 −
+-------------+ | SubjectCode | +-------------+ | Java899@22 | | C# | | ~Python232 | | MongoDB% | +-------------+ 4 rows in set (0.00 sec)