MySQL 查询仅返回数字行?\
mysqlmysqli database
使用 REGEXP 仅返回数字行。让我们首先创建一个表 −
mysql> create table DemoTable -> ( -> StudentId varchar(100) -> ); Query OK, 0 rows affected (0.51 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('John74747'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('8494575Carol'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('985755645'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Carol-9032'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('101'); Query OK, 1 row affected (0.17 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
输出
+--------------+ | StudentId | +--------------+ | John74747 | | 8494575Carol | | 985755645 | | Carol-9032 | | 101 | +--------------+ 5 rows in set (0.00 sec)
以下查询仅返回数字行 −
mysql> select *from DemoTable where StudentId REGEXP '^[0-9]+$';
输出
+-----------+ | StudentId | +-----------+ | 985755645 | | 101 | +-----------+ 2 rows in set (0.17 sec)