Regex to find string and next character in a comma separated list - MySQL?

MySQLMySQLi Database

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)
raja
Published on 19-Nov-2020 11:24:20
Advertisements