
- 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
MySQL query to select a row which contains same number in a column with set of numbers separated by comma?
You need to use FIND_IN_SET() for this. Let us first create a table −
mysql> create table DemoTable ( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, CustomerName varchar(20), CustomerAllProductPrice text ); Query OK, 0 rows affected (0.30 sec)
Insert some records in the table using insert command. Here, we are inserting numbers separated by comma −
mysql> insert into DemoTable(CustomerName,CustomerAllProductPrice) values('Chris','245,345,678,90,45,56,78'); Query OK, 1 row affected (0.03 sec) mysql> insert into DemoTable(CustomerName,CustomerAllProductPrice) values('Chris','98,99,90,56,77'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(CustomerName,CustomerAllProductPrice) values('David','1000,2000,4000,56000'); Query OK, 1 row affected (0.06 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------+--------------+-------------------------+ | CustomerId | CustomerName | CustomerAllProductPrice | +------------+--------------+-------------------------+ | 1 | Chris | 245,345,678,90,45,56,78 | | 2 | Chris | 98,99,90,56,77 | | 3 | David | 1000,2000,4000,56000 | +------------+--------------+-------------------------+ 3 rows in set (0.00 sec)
Following is the query to select a row which contains same number in column with set of numbers separated by comma −
mysql> select *from DemoTable where find_in_set('4000',CustomerAllProductPrice);
This will produce the following output −
+------------+--------------+-------------------------+ | CustomerId | CustomerName | CustomerAllProductPrice | +------------+--------------+-------------------------+ | 3 | David | 1000,2000,4000,56000 | +------------+--------------+-------------------------+ 1 row in set (0.14 sec)
- Related Articles
- Can I search for particular numbers in a MySQL column with comma separated records using a MySQL query?
- Display all the column values in a single row separated by comma in MySQL?
- Finding a specific row which has several ids separated by comma in MySQL?
- MySQL query to find a value in a set of values separated by comma in a custom variable
- How to display data from a MySQL column separated by comma?
- Searching from a comma separated MySQL column?
- How to sum a comma separated string (string with numbers) in MySQL?
- How to include quotes in comma separated column with MySQL?
- Find specific records from a column with comma separated values in MySQL
- MySQL query to order by the first number in a set of numbers?
- Get values from all rows and display it a single row separated by comma with MySQL
- MySQL query to get string from one column and find its position in another column with comma separated values?
- How to set a comma separated list as a table in MySQL?
- MySQL query to concatenate records with similar corresponding ids in a single row separated by a special character
- Count the number of comma’s in every record from a comma-separated value column in MySQL

Advertisements