How to use MySQL LIKE to create a search system and display results on the basis of keyword?


To display results on the basis of keyword, use the LIKE operator as in the following syntax −

select *from yourTableName where yourColumnName LIKE '%yourValue%';

Let us first create a table −

mysql> create table DemoTable812(Comments text);
Query OK, 0 rows affected (0.61 sec)

Insert some records in the table using insert command −

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)

Display all records from the table using select statement −

mysql> select *from DemoTable812;

This will produce the following output −

+------------------+
| Comments         |
+------------------+
| Good Looking     |
| Awesome          |
| Looking Gorgeous |
| Good Luck        |
| Have a nice day  |
+------------------+
5 rows in set (0.00 sec)

Following is the query to use MySQL LIKE to create a search system and display results on the basis of keyword −

mysql> select *from DemoTable812 where Comments LIKE '%Look%';

This will produce the following output −

+------------------+
| Comments         |
+------------------+
| Good Looking     |
| Looking Gorgeous |
+------------------+
2 rows in set (0.00 sec)

Updated on: 03-Sep-2019

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements