如何使用 MySQL LIKE 提取设置了多值的字段?


要在 MySQL 中使用带 OR 的 LIKE 提取带有多个值的字段 -

select *from yourTableName where yourColumnName like ‘%AnyStringValue’ or yourColumnName like ‘%AnyStringValue’ or yourColumnName like ‘%AnyStringValue’ ……...N;

你可以通过一个表来理解 -

mysql> create table LikeDemo
   −> (
      −> Hobby varchar(200)
   −> );
Query OK, 0 rows affected (1.71 sec)

在表中插入一些记录,使用 insert 命令。在表中插入记录的查询如下 -

mysql> insert into LikeDemo values('Reading Book');
Query OK, 1 row affected (0.13 sec)

mysql> insert into LikeDemo values('Playing Cricket Match');
Query OK, 1 row affected (0.16 sec)

mysql> insert into LikeDemo values('Playing Hockey Match');
Query OK, 1 row affected (0.27 sec)

mysql> insert into LikeDemo values('Reading Novel');
Query OK, 1 row affected (0.14 sec)

mysql> insert into LikeDemo values('Swimming');
Query OK, 1 row affected (0.10 sec)

Displaying all records with the help of select statement. The query is as follows:

mysql> select *from LikeDemo;

以下是输出 -

+-----------------------+
| Hobby                 |
+-----------------------+
| Reading Book          |
| Playing Cricket Match |
| Playing Hockey Match  |
| Reading Novel         |
| Swimming              |
+-----------------------+
5 rows in set (0.00 sec)

使用 LIKE 提取具有多个值的字段的查询如下 -

mysql> select *from LikeDemo where Hobby like '%Cricket%' or Hobby like '%Reading%';

以下是输出 -

+-----------------------+
| Hobby                 |
+-----------------------+
| Reading Book          |
| Playing Cricket Match |
| Reading Novel         |
+-----------------------+
3 rows in set (0.00 sec)

更新于: 25-Jun-2020

646 浏览量

开始您的 职业生涯

完成课程获取认证

开始
广告