
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Check if value exists in a comma separated list in MySQL?
To check if value exists in a comma separated list, you can use FIND_IN_SET() function.
The syntax is as follows
SELECT *FROM yourTablename WHERE FIND_IN_SET(‘yourValue’,yourColumnName) > 0;
Let us first create a table. The query to create a table is as follows
mysql> create table existInCommaSeparatedList - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > Name varchar(200) - > ); Query OK, 0 rows affected (0.68 sec)
Now you can insert some records in the table using insert command.
The query is as follows
mysql> insert into existInCommaSeparatedList(Name) values('John,Carol,Sam,Larry,Bob,David'); Query OK, 1 row affected (0.35 sec) mysql> insert into existInCommaSeparatedList(Name) values('Maxwell,Chris,James'); Query OK, 1 row affected (0.14 sec) mysql> insert into existInCommaSeparatedList(Name) values('Robert,Ramit'); Query OK, 1 row affected (0.34 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from existInCommaSeparatedList;
The following is the output
+----+--------------------------------+ | Id | Name | +----+--------------------------------+ | 1 | John,Carol,Sam,Larry,Bob,David | | 2 | Maxwell,Chris,James | | 3 | Robert,Ramit | +----+--------------------------------+ 3 rows in set (0.00 sec)
Here is the query to check if value exists in a comma separated list. We are checking for the field with comma separated text “Robert”
mysql> SELECT *FROM existInCommaSeparatedList WHERE FIND_IN_SET('Robert',Name) > 0;
The following is the output
+----+--------------+ | Id | Name | +----+--------------+ | 3 | Robert,Ramit | +----+--------------+ 1 row in set (0.00 sec)
- Related Articles
- How can I match a comma separated list against a value in MySQL?
- Display MySQL Results as comma separated list?
- Check if a value exists in a column in a MySQL table?
- How to set a comma separated list as a table in MySQL?
- Display duplicate record as a distinct value with corresponding values as distinct comma separated list in MySQL?
- Regex to find string and next character in a comma separated list - MySQL?
- Convert String into comma separated List in Java
- Check if a list exists in given list of lists in Python
- Check if a particular value exists in Java LinkedHashMap
- Check if a particular value exists in Java TreeSet
- Searching from a comma separated MySQL column?
- How to check if value exists with MySQL SELECT 1?
- Count values from comma-separated field in MySQL?
- MySQL query to retrieve records from the part of a comma-separated list?
- Remove specific word in a comma separated string with MySQL

Advertisements