
- 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 count comma’s from field value?
Following is the syntax −
select length(yourColumnName) - length(replace(yourColumnName, ',', '')) as anyAliasName from yourTableName;
Let us first create a table −
mysql> create table DemoTable1510 -> ( -> Value varchar(50) -> ); Query OK, 0 rows affected (6.75 sec)
Insert some records in the table using insert command −
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 statement −
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 from field value −
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
- Count the number of comma’s in every record from a comma-separated value column in MySQL
- Count boolean field values within a single MySQL query?
- MySQL query to extract last word from a field?
- MySQL query to extract first word from a field?
- MySQL query to return the count of only NO values from corresponding column value
- MySQL query to get the count of all the elements in the field?
- Count values from comma-separated field in MySQL?
- How to derive value of a field from another field in MySQL?
- MySQL query to get a field value that does not contain empty spaces?
- MySQL query to get the max value with numeric values in varchar field?
- MySQL query to replace special characters from column value
- MongoDB query to create new field and count set the count of another field in it?
- MySQL query to fetch only a single field on the basis of boolean value in another field
- How to count rows from two tables in a single MySQL query?
- Remove 20% from stored price in MySQL field, using SQL query?

Advertisements