 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements
                    