返回僅為數值的行的 MySQL 查詢?\n


使用 REGEXP 僅返回數字行。我們首先建立一個表 -

mysql> create table DemoTable
   -> (
   -> StudentId varchar(100)
   -> );
Query OK, 0 rows affected (0.51 sec)

使用插入命令向表中插入一些記錄 -

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)

更新於: 2019-07-30

862 瀏覽次數

開啟您的事業之路

完成課程並獲得認證

開始學習
廣告
© . All rights reserved.