Select records from a table on the basis of keywords in MySQL

MySQLMySQLi Database

Let’s say some of the columns values in a table has a specific keyword and you want only those records. For this, use the LIKE operator.

Let us first see an example and create a table −

mysql> create table DemoTable750 (Title varchar(200));
Query OK, 0 rows affected (0.69 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable750 values('Java and MongoDB,MySQL');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable750 values('MySQL,SQL Server');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable750 values('PL/SQL,Python,MongoDB');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable750 values('MySQL');
Query OK, 1 row affected (0.33 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable750

This will produce the following output -

+------------------------+
| Title                  |
+------------------------+
| Java and MongoDB,MySQL |
| MySQL,SQL Server       |
| PL/SQL,Python,MongoDB  |
| MySQL                  |
+------------------------+
4 rows in set (0.00 sec)

Following is the query to select records on the basis of a keyword. Let’s say the keyword is “MySQL” −

mysql> select *from DemoTable750 where Title LIKE '%MySQL%';

This will produce the following output -

+------------------------+
| Title                  |
+------------------------+
| Java and MongoDB,MySQL |
| MySQL,SQL Server       |
| MySQL                  |
+------------------------+
3 rows in set (0.00 sec)
raja
Published on 03-Sep-2019 07:40:36
Advertisements