- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use a comma-separated string in an `IN ()` in MySQL?
Set the comma-separated string in the IN() as in the below syntax:
select *from yourTableName where yourColumnName IN('yourCommaSeparatedValue');
Let us first create a table −
mysql> create table DemoTable1314 -> ( -> Number varchar(100) -> ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1314 values('45,67,89'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1314 values('10,20,50'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1314 values('90,56,45'); Query OK, 1 row affected (0.23 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1314;
Output
+----------+ | Number | +----------+ | 45,67,89 | | 10,20,50 | | 90,56,45 | +----------+ 3 rows in set (0.00 sec)
Following is the query to use a comma-separated string in MySQL `IN ()`:
mysql> select *from DemoTable1314 where Number IN('10,20,50');
Output
+----------+ | Number | +----------+ | 10,20,50 | +----------+ 1 row in set (0.02 sec)
Advertisements