Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

How to fetch fields with multiple values set using MySQL LIKE?



To fetch fields with multiple values, use LIKE with OR in MySQL −

select *from yourTableName where yourColumnName like ‘%AnyStringValue’ or yourColumnName like ‘%AnyStringValue’ or yourColumnName like ‘%AnyStringValue’ ……...N;

You can understand with the help of a table −

mysql> create table LikeDemo
   −> (
      −> Hobby varchar(200)
   −> );
Query OK, 0 rows affected (1.71 sec)

Insert some records in the table with the help of insert command. The query to insert records in the table is as follows −

mysql> insert into LikeDemo values('Reading Book');
Query OK, 1 row affected (0.13 sec)

mysql> insert into LikeDemo values('Playing Cricket Match');
Query OK, 1 row affected (0.16 sec)

mysql> insert into LikeDemo values('Playing Hockey Match');
Query OK, 1 row affected (0.27 sec)

mysql> insert into LikeDemo values('Reading Novel');
Query OK, 1 row affected (0.14 sec)

mysql> insert into LikeDemo values('Swimming');
Query OK, 1 row affected (0.10 sec)

Displaying all records with the help of select statement. The query is as follows:

mysql> select *from LikeDemo;

The following is the output −

+-----------------------+
| Hobby                 |
+-----------------------+
| Reading Book          |
| Playing Cricket Match |
| Playing Hockey Match  |
| Reading Novel         |
| Swimming              |
+-----------------------+
5 rows in set (0.00 sec)

The query to fetch fields with multiple values using LIKE is as follows −

mysql> select *from LikeDemo where Hobby like '%Cricket%' or Hobby like '%Reading%';

The following is the output −

+-----------------------+
| Hobby                 |
+-----------------------+
| Reading Book          |
| Playing Cricket Match |
| Reading Novel         |
+-----------------------+
3 rows in set (0.00 sec)
Updated on: 2020-06-25T11:18:08+05:30

765 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements