MySQL Select where values IN List string?


For this, use FIND_IN_SET().

Let us create a table −

Example

mysql> create table demo81
   -> (
   -> id int not null auto_increment primary key,
   -> username varchar(200)
   -> );
Query OK, 0 rows affected (1.44

Insert some records into the table with the help of insert command −

Example

mysql> insert into demo81(username) values('John,Chris,David');
Query OK, 1 row affected (0.11

mysql> insert into demo81(username) values('Mike,Sam');
Query OK, 1 row affected (0.14

mysql> insert into demo81(username) values('Chris,Bob,Sam');
Query OK, 1 row affected (0.13

mysql> insert into demo81(username) values('Mike,John,Chris');
Query OK, 1 row affected (0.23

Display records from the table using select statement −

Example

mysql> select *from demo81;

This will produce the following output −

Output

+----+------------------+

| id | username         |

+----+------------------+

|  1 | John,Chris,David |

|  2 | Mike,Sam         |

|  3 | Chris,Bob,Sam    |

|  4 | Mike,John,Chris  |

+----+------------------+

4 rows in set (0.00 sec)

Following is the query to select where values IN list string −

Example

mysql> select *from demo81 where find_in_set('Chris',username) > 0;

This will produce the following output −

Output

+----+------------------+

| id | username         |

+----+------------------+

|  1 | John,Chris,David |

|  3 | Chris,Bob,Sam    |

|  4 | Mike,John,Chris  |

+----+------------------+

3 rows in set (0.03 sec)

Updated on: 11-Dec-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements