
- 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
Count the number of comma’s in every record from a comma-separated value column in MySQL
Let us first create a −
mysql> create table DemoTable1510 -> ( -> Value varchar(50) -> ); Query OK, 0 rows affected (6.75 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1510 values('20,35'); Query OK, 1 row affected (0.57 sec) mysql> insert into DemoTable1510 values('45,67,89'); Query OK, 1 row affected (0.99 sec) mysql> insert into DemoTable1510 values('90,97,101,190'); Query OK, 1 row affected (1.15 sec)
Display all records from the table using select −
mysql> select * from DemoTable1510;
This will produce the following output −
+---------------+ | Value | +---------------+ | 20,35 | | 45,67,89 | | 90,97,101,190 | +---------------+ 3 rows in set (0.00 sec)
Here is the query to count comma’s −
mysql> select length(Value) - length(replace(Value, ',', '')) as NumberOfComma from DemoTable1510;
This will produce the following output −
+---------------+ | NumberOfComma | +---------------+ | 1 | | 2 | | 3 | +---------------+ 3 rows in set (0.00 sec)
- Related Articles
- Searching from a comma separated MySQL column?
- Count values from comma-separated field in MySQL?
- MySQL query to count comma’s from field value?
- Find specific records from a column with comma separated values in MySQL
- How to display data from a MySQL column separated by comma?
- Display duplicate record as a distinct value with corresponding values as distinct comma separated list in MySQL?
- How to count specific comma separated values in a row retrieved from MySQL database?
- Check if value exists in a comma separated list in MySQL?
- MySQL query to get a single value from position of comma-separated string?
- How to include quotes in comma separated column with MySQL?
- Display all the column values in a single row separated by comma in MySQL?
- Fetch records from comma separated values using MySQL IN()?
- How can I match a comma separated list against a value in MySQL?
- Count number of times value appears in particular column in MySQL?
- MySQL query to select a row which contains same number in a column with set of numbers separated by comma?

Advertisements