我們能否結合使用 MySQL IN 和 LIKE 運算子?


是的,我們可以使用 LIKE 和 OR 運算子在 MySQL 中組合 IN 和 LIKE 運算子。我們首先建立一個表 −

mysql> create table DemoTable
-> (
-> SubjectTitle text
-> );
Query OK, 0 rows affected (0.68 sec)

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

mysql> insert into DemoTable values('Introduction To MySQL');
Query OK, 1 row affected (0.11 sec)

mysql> insert into DemoTable values('Java in Depth');
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values('Coding with Python');
Query OK, 1 row affected (0.54 sec)

mysql> insert into DemoTable values('C++ in Depth');
Query OK, 1 row affected (0.17 sec)

使用 select 語句從表中顯示所有記錄 −

mysql> select *from DemoTable;

輸出

這將產生以下輸出 −

+-----------------------+
| SubjectTitle          |
+-----------------------+
| Introduction To MySQL |
| Java in Depth         |
| Coding with Python    |
| C++ in Depth          |
+-----------------------+
4 rows in set (0.00 sec)

以下是在單個 MySQL 查詢中使用 LIKE 和 OR 的查詢 −

mysql> select *from DemoTable where SubjectTitle LIKE '%MySQL%' OR SubjectTitle LIKE'C++%';

輸出

這將產生以下輸出 −

+-----------------------+
| SubjectTitle          |
+-----------------------+
| Introduction To MySQL |
| C++ in Depth          |
+-----------------------+
2 rows in set (0.00 sec)

更新於: 30-Jun-2020

618 次瀏覽

開啟你的 職業 生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.