
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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
Regex to find string and next character in a comma separated list - MySQL?
To search in a comma separated list, use MySQL find_in_set(). The usage of Regex for this purpose isnβt required here. The syntax is as follows β
select *from yourTableName where find_in_set(anyValue,yourColumnName);
Let us create a table β
mysql> create table demo17 β> ( β> id int not null auto_increment primary key, β> first_name varchar(50), β> value text β> ); Query OK, 0 rows affected (1.81 sec)
Insert some records into the table with the help of insert command β
mysql> insert into demo17(first_name,value) values('John','50'); Query OK, 1 row affected (0.11 sec) mysql> insert into demo17(first_name,value) values('David',''); Query OK, 1 row affected (0.13 sec) mysql> insert into demo17(first_name,value) values('Adam','50,89'); Query OK, 1 row affected (0.14 sec) mysql> insert into demo17(first_name,value) values('Adam','49,89,43'); Query OK, 1 row affected (0.20 sec)
Display records from the table using select statement β
mysql> select *from demo17;
This will produce the following output β
+----+------------+----------+ | id | first_name | value | +----+------------+----------+ | 1 | John | 50 | | 2 | David | | | 3 | Adam | 50,89 | | 4 | Adam | 49,89,43 | +----+------------+----------+ 4 rows in set (0.00 sec)
Following is the query to find string and next character β
mysql> select *from demo17 where find_in_set(50,value);
This will produce the following output β
+----+------------+-------+ | id | first_name | value | +----+------------+-------+ | 1 | John | 50 | | 3 | Adam | 50,89 | +----+------------+-------+ 2 rows in set (0.00 sec)
Advertisements