要使用LIKE提取多个值,请将LIKE运算符与OR运算符一起使用。让我们首先创建一个表-
mysql> create table DemoTable1027 ( Id int, Name varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1027 values(100,'John'); mysql> insert into DemoTable1027 values(20,'Chris'); mysql> insert into DemoTable1027 values(200,'Robert'); mysql> insert into DemoTable1027 values(400,'Mike');
使用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)