是否可以在 MySQL Select 语句中允许正则匹配?
是的,我们可以在 select 语句中进行正则匹配 −
select yourColumnName from yourTableName where yourColumnName regexp '^yourValue';
我们首先创建一个表 −
mysql> create table DemoTable1892 ( FirstName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1892 values('John'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1892 values('Adam'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1892 values('Jace'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1892 values('Johny'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1892 values('Johnson'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1892 values('Jack'); Query OK, 1 row affected (0.00 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1892;
这将生成以下输出 −
+-----------+ | FirstName | +-----------+ | John | | Adam | | Jace | | Johny | | Johnson | | Jack | +-----------+ 6 rows in set (0.00 sec)
以下是 MySQL select 语句中正则匹配的查询 −
mysql> select FirstName from DemoTable1892 where FirstName regexp '^Jo';
这将生成以下输出 −
+-----------+ | FirstName | +-----------+ | John | | Johny | | Johnson | +-----------+ 3 rows in set (0.00 sec)
广告