如何使用 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 瀏覽量

開始您的 職業生涯

完成課程獲取認證

開始
廣告