- 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 find a specific record from a list of values with semicolon in MySQL?
For this, you can use FIND_IN_SET(). Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value varchar(100) ); Query OK, 0 rows affected (2.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Value) values('100;200;300'); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable(Value) values('1;300;400'); Query OK, 1 row affected (0.58 sec) mysql> insert into DemoTable(Value) values('6;7;8;9;10'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable(Value) values('1;2;3;4;5'); Query OK, 1 row affected (9.36 sec) mysql> insert into DemoTable(Value) values('3;8;9;10'); Query OK, 1 row affected (0.46 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-------------+ | Id | Value | +----+-------------+ | 1 | 100;200;300 | | 2 | 1;300;400 | | 3 | 6;7;8;9;10 | | 4 | 1;2;3;4;5 | | 5 | 3;8;9;10 | +----+-------------+ 5 rows in set (0.00 sec)
Following is the query to find a specific record from a list of values with a semicolon −
mysql> select *from DemoTable where find_in_set(8, replace(Value, ';', ',')) > 0;
This will produce the following output −
+----+------------+ | Id | Value | +----+------------+ | 3 | 6;7;8;9;10 | | 5 | 3;8;9;10 | +----+------------+ 2 rows in set (0.00 sec)
Advertisements