如何在 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.