在单个 MySQL 查询中使用 LIKE % 提取多个值


要提取 LIKE 中的多个值,请将 LIKE 运算符与 OR 运算符一起使用。我们首先创建一个表——

mysql> create table DemoTable1027
(
   Id int,
   Name varchar(100)
);
Query OK, 0 rows affected (1.64 sec)

使用插入命令在表中插入一些记录——

mysql> insert into DemoTable1027 values(100,'John');
Query OK, 1 row affected (0.72 sec)
mysql> insert into DemoTable1027 values(20,'Chris');
Query OK, 1 row affected (0.56 sec)
mysql> insert into DemoTable1027 values(200,'Robert');
Query OK, 1 row affected (0.84 sec)
mysql> insert into DemoTable1027 values(400,'Mike');
Query OK, 1 row affected (0.47 sec)

使用 select 语句显示表中的所有记录——

mysql> select *from DemoTable1027;

这将产生以下输出——

+------+--------+
| Id   | Name   |  
+------+--------+
| 100  | John   |
| 20   | Chris  |
| 200  | Robert |
| 400  | Mike   |
+------+--------+
4 rows in set (0.00 sec)

以下是要使用 LIKE 并提取多个记录的查询——

mysql> select *from DemoTable1027 where Id LIKE '%100%' or Id LIKE '%200%';

这将产生以下输出——

+------+--------+
| Id   | Name   |
+------+--------+
| 100  | John   |
| 200  | Robert |
+------+--------+
2 rows in set (0.74 sec)

更新于:30-Sep-2019

3K+ 浏览

开始你的 职业生涯

完成课程以获得认证

开始
广告
© . All rights reserved.