如何在 MySQL 中选择以特定值开头的记录?
mysqlmysqli database更新于 2025/10/21 17:48:17
要选择以特定值开头的记录,您需要使用 LIKE 运算符。让我们首先创建一个表 −
mysql> create table DemoTable690( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserValue varchar(100) ); Query OK, 0 rows affected (0.56 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable690(UserValue) values('567890');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable690(UserValue) values('789032');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable690(UserValue) values('567342');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable690(UserValue) values('890678');
Query OK, 1 row affected (0.16 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable690;
这将产生以下输出 −
+--------+-----------+ | UserId | UserValue | +--------+-----------+ | 1 | 567890 | | 2 | 789032 | | 3 | 567342 | | 4 | 890678 | +--------+-----------+ 4 rows in set (0.00 sec)
以下是选择以特定值开头的记录的查询 −
mysql> select *from DemoTable690 where UserValue LIKE '567%';
这将产生以下输出 −
+--------+-----------+ | UserId | UserValue | +--------+-----------+ | 1 | 567890 | | 3 | 567342 | +--------+-----------+ 2 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/
打印
下一节:由于我们不能使用保留字作为表名,因此"create table table"在 MySQL 中是否可以工作? ❯❮ 上一节:如何修复错误"您的语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,了解附近要使用的正确语法……"?

