MySQL正则表达式:如何用d匹配字符串中的数字?
要在字符串中匹配数字,在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)
阅读更多:MySQL 教程