使用 REGEXP 匹配每条记录后的可选行尾?
mysqlmysqli database更新于 2025/10/4 17:16:17
首先我们创建一个表 −
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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

