如何使用 MySQL LIKE 建立搜尋系統,並根據關鍵字顯示結果?
要根據關鍵字顯示結果,請使用 LIKE 運算子,語法如下 −
select *from yourTableName where yourColumnName LIKE '%yourValue%';
我們首先建立一個表 −
mysql> create table DemoTable812(Comments text); Query OK, 0 rows affected (0.61 sec)
使用 insert 命令在表中插入一些記錄 −
mysql> insert into DemoTable812 values('Good Looking');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable812 values('Awesome');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable812 values('Looking Gorgeous');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable812 values('Good Luck');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable812 values('Have a nice day');
Query OK, 1 row affected (0.21 sec)使用 select 語句顯示錶中的所有記錄 −
mysql> select *from DemoTable812;
這將產生以下輸出 −
+------------------+ | Comments | +------------------+ | Good Looking | | Awesome | | Looking Gorgeous | | Good Luck | | Have a nice day | +------------------+ 5 rows in set (0.00 sec)
以下查詢使用 MySQL LIKE 建立搜尋系統,並根據關鍵字顯示結果 −
mysql> select *from DemoTable812 where Comments LIKE '%Look%';
這將產生以下輸出 −
+------------------+ | Comments | +------------------+ | Good Looking | | Looking Gorgeous | +------------------+ 2 rows in set (0.00 sec)
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP