MySQL 正则表达式:如何用 d 匹配字符串中的数字?

mysqlmysqli database更新于 2024/10/13 15:08:17

要匹配字符串中的数字,MySQL 中的语法如下 −

select * from yourTab leName where yourColumnName regexp '[0-9]';

为了理解上述语法,让我们创建一个表 −

mysql> create table DemoTable1841
     (
     Code varchar(20)
     );
Query OK, 0 rows affected (0.00 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable1841 values('John231');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1841 values('19876Sam');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1841 values('Carol');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1841 values('67474');
Query OK, 1 row affected (0.00 sec)

使用 select 语句显示表中的所有记录 −

mysql> select * from DemoTable1841;

这将产生以下输出 −

+----------+
| Code     |
+----------+
| John231  |
| 19876Sam |
| Carol    |
| 67474    |
+----------+
4 rows in set (0.00 sec)

这是匹配字符串中数字的正则表达式 −

mysql> select * from DemoTable1841 where Code regexp '[0-9]';

这将产生以下输出 −

+----------+
| Code     |
+----------+
| John231  |
| 19876Sam |
| 67474    |
+----------+
3 rows in set (0.00 sec)

相关文章


有用资源