使用 LIKE 和 OR 進行文字搜尋的 MySQL 查詢以獲取記錄
我們首先建立一個表 −
mysql> create table DemoTable ( Subject text ); Query OK, 0 rows affected (0.86 sec)
使用 insert 命令在表中插入一些記錄 −
mysql> insert into DemoTable values('Introduction to MySQL');
Query OK, 1 row affected (0.31 sec)
mysql> insert into DemoTable values('Deep Dive using Java');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('C in Depth');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Introduction to C++');
Query OK, 1 row affected (0.48 sec)使用 select 語句從表中顯示所有記錄 −
mysql> select *from DemoTable;
這將產生以下輸出 −
+-----------------------+ | Subject | +-----------------------+ | Introduction to MySQL | | Deep Dive using Java | | C in Depth | | Introduction to C++ | +-----------------------+ 4 rows in set (0.00 sec)
以下是使用 LIKE 和 OR 進行文字搜尋的查詢 −
mysql> select *from DemoTable where Subject LIKE '%MySQL%' OR Subject LIKE '%using%';
這將產生以下輸出 −
+-----------------------+ | Subject | +-----------------------+ | Introduction to MySQL | | Deep Dive using Java | +-----------------------+ 2 rows in set (0.00 sec)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP