使用 REGEXP 匹配每条记录后的可选行尾?
我们先创建一个表 -
mysql> create table DemoTable(EmployeeCode varchar(100)); Query OK, 0 rows affected (0.56 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values('EMPLOYEE:100 John Smith'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('EMPLOYEE:16537 Chris Brown'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('EMPLOYEE:100 David Miller'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('EMPLOYEE:100 23432 David Miller'); Query OK, 1 row affected (0.20 sec)
使用 select 语句显示表中的所有记录 -
mysql>; select *from DemoTable;
这将生成以下输出 -
+---------------------------------+ | EmployeeCode | +---------------------------------+ | EMPLOYEE:100 John Smith | | EMPLOYEE:16537 Chris Brown | | EMPLOYEE:100 David Miller | | EMPLOYEE:100 23432 David Miller | +---------------------------------+ 4 rows in set (0.00 sec)
以下是对 “EMPLOYEE:100 23432 David Miller” 中的 100 后的可选行尾进行匹配的查询,例如 23434 -
mysql> select *from DemoTable where EmployeeCode REGEXP "EMPLOYEE:100([^0-9]|$)";
这将生成以下输出 -
+---------------------------------+ | EmployeeCode | +---------------------------------+ | EMPLOYEE:100 John Smith | | EMPLOYEE:100 David Miller | | EMPLOYEE:100 23432 David Miller | +---------------------------------+ 3 rows in set (0.00 sec)
广告