
- 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
Multiple COUNT() for multiple conditions in a single MySQL query?
You can count multiple COUNT() for multiple conditions in a single query using GROUP BY.
The syntax is as follows -
SELECT yourColumnName,COUNT(*) from yourTableName group by yourColumnName;
To understand the above syntax, let us first create a table. The query to create a table is as follows.
mysql> create table MultipleCountDemo -> ( -> Id int, -> Name varchar(100), -> Age int -> ); Query OK, 0 rows affected (2.17 sec)
Insert records in the table using insert command. The query is as follows.
mysql> insert into MultipleCountDemo values(1,'Carol',21); Query OK, 1 row affected (0.27 sec) mysql> insert into MultipleCountDemo values(2,'Sam',21); Query OK, 1 row affected (0.29 sec) mysql> insert into MultipleCountDemo values(3,'Bob',22); Query OK, 1 row affected (0.10 sec) mysql> insert into MultipleCountDemo values(4,'John',23); Query OK, 1 row affected (0.24 sec) mysql> insert into MultipleCountDemo values(5,'David',22); Query OK, 1 row affected (0.12 sec) mysql> insert into MultipleCountDemo values(6,'Adam',22); Query OK, 1 row affected (0.21 sec) mysql> insert into MultipleCountDemo values(7,'Johnson',23); Query OK, 1 row affected (0.14 sec) mysql> insert into MultipleCountDemo values(8,'Elizabeth',23); Query OK, 1 row affected (0.25 sec)
Display all records from the table using select statement. The query is as follows -
mysql> select *from MultipleCountDemo;
The following is the output.
+------+-----------+------+ | Id | Name | Age | +------+-----------+------+ | 1 | Carol | 21 | | 2 | Sam | 21 | | 3 | Bob | 22 | | 4 | John | 23 | | 5 | David | 22 | | 6 | Adam | 22 | | 7 | Johnson | 23 | | 8 | Elizabeth | 23 | +------+-----------+------+ 8 rows in set (0.00 sec)
Now here is the query for multiple count() for multiple conditions in a single query.
mysql> select Age,count(*)as AllSingleCount from MultipleCountDemo group by Age;
The following is the output.
+------+----------------+ | Age | AllSingleCount | +------+----------------+ | 21 | 2 | | 22 | 3 | | 23 | 3 | +------+----------------+ 3 rows in set (0.00 sec)
- Related Articles
- Implement multiple COUNT() in a single MySQL query
- Get multiple count in a single MySQL query for specific column values
- Change multiple columns in a single MySQL query?
- Insert multiple rows in a single MySQL query
- MySQL update multiple records in a single query?
- MySQL query to increase item value price for multiple items in a single query?
- Implement multiple LIKE operators in a single MySQL query
- MySQL query to sort multiple columns together in a single query
- How to write a single MySQL query for displaying a value for multiple inputs?
- How to get multiple rows in a single MySQL query?
- How to obtain multiple rows in a single MySQL query?
- Multiple Inserts for a single column in MySQL?
- MySQL query to count rows in multiple tables
- Use LIKE % to fetch multiple values in a single MySQL query
- How to insert multiple rows with single MySQL query?

Advertisements