Use LIKE % to fetch multiple values in a single MySQL query

MySQLMySQLi Database

To fetch multiple values wit LIKE, use the LIKE operator along with OR operator. Let us first create a table −

mysql> create table DemoTable1027
(
   Id int,
   Name varchar(100)
);
Query OK, 0 rows affected (1.64 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1027 values(100,'John');
Query OK, 1 row affected (0.72 sec)
mysql> insert into DemoTable1027 values(20,'Chris');
Query OK, 1 row affected (0.56 sec)
mysql> insert into DemoTable1027 values(200,'Robert');
Query OK, 1 row affected (0.84 sec)
mysql> insert into DemoTable1027 values(400,'Mike');
Query OK, 1 row affected (0.47 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable1027;

This will produce the following output −

+------+--------+
| Id   | Name   |  
+------+--------+
| 100  | John   |
| 20   | Chris  |
| 200  | Robert |
| 400  | Mike   |
+------+--------+
4 rows in set (0.00 sec)

Following is the query to use LIKE and fetch multiple records −

mysql> select *from DemoTable1027 where Id LIKE '%100%' or Id LIKE '%200%';

This will produce the following output −

+------+--------+
| Id   | Name   |
+------+--------+
| 100  | John   |
| 200  | Robert |
+------+--------+
2 rows in set (0.74 sec)
raja
Published on 30-Sep-2019 06:58:07
Advertisements