如何在 MySQL 中選擇以特定數字開頭的記錄?


選擇以特定數字開頭的記錄的最佳解決方案是使用 MySQL LIKE 運算子。我們先建立一個表 -

mysql> create table DemoTable
(
   ClientId bigint,
   ClientName varchar(40)
);
Query OK, 0 rows affected (0.82 sec)

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

mysql> insert into DemoTable values(23568777,'Chris Brown');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(9085544,'John Doe');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(9178432,'John Doe');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values(9078482,'David Miller');
Query OK, 1 row affected (0.17 sec)

使用 select 語句從表中顯示所有記錄 -

mysql> select *from DemoTable;

這將產生以下輸出 -

+----------+--------------+
| ClientId | ClientName   |
+----------+--------------+
| 23568777 | Chris Brown  |
| 9085544  | John Doe     |
| 9178432  | John Doe     |
| 9078482  | David Miller |
+----------+--------------+
4 rows in set (0.00 sec)

以下是 MySQL 中選擇以特定數字開頭的記錄的查詢 -

mysql> select *from DemoTable where ClientId LIKE '90%';

這將產生以下輸出 -

+----------+--------------+
| ClientId | ClientName   |
+----------+--------------+
| 9085544  | John Doe     |
| 9078482  | David Miller |
+----------+--------------+
2 rows in set (0.00 sec)

最後更新於:09-Oct-2019

3000+ 次瀏覽

開啟您的職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.