用於匹配列值中任意兩個字串的 MySQL 查詢


基於這個,你可以將 LIKE 運算子與 OR 條件結合使用。

首先讓我們建立一個表:

mysql> create table DemoTable762 (Title text);
Query OK, 0 rows affected (0.54 sec)

使用 insert 命令在表中插入一些記錄:

mysql> insert into DemoTable762 values('Introduction to Java');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable762 values('MySQL is a RDBMS');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable762 values('Data Structure and Algorithm in Java');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable762 values('Data Structure and Algorithm in C and C++');
Query OK, 1 row affected (0.18 sec)

使用 select 語句顯示錶中的所有記錄:

mysql> select *from DemoTable762 ;

這將生成以下輸出 -

+-------------------------------------------+
| Title                                     |
+-------------------------------------------+
| Introduction to Java                      |
| MySQL is a RDBMS                          |
| Data Structure and Algorithm in Java      |
| Data Structure and Algorithm in C and C++ |
+-------------------------------------------+
4 rows in set (0.00 sec)

以下是查詢以匹配 “MySQL” 或 “Algorithm”這兩個字串中的任何一個:

mysql> select *from DemoTable762 where Title LIKE '%MySQL%' or Title LIKE '%Algorithm%';

這將生成以下輸出 -

+-------------------------------------------+
| Title                                     |
+-------------------------------------------+
| MySQL is a RDBMS                          |
| Data Structure and Algorithm in Java      |
| Data Structure and Algorithm in C and C++ |
+-------------------------------------------+
3 rows in set (0.00 sec)

更新於:2019 年 9 月 3 日

624 次瀏覽

開啟您的 事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.