
- 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
Implement multiple COUNT() in a single MySQL query
For this, use CASE statement. Let us first create a table −
mysql> create table DemoTable -> ( -> LastName varchar(100) -> ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Brown'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Brown'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Smith'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Taylor'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Taylor'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Miller'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Miller'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Miller'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+----------+ | LastName | +----------+ | Smith | | Brown | | Brown | | Smith | | Taylor | | Taylor | | Miller | | Miller | | Miller | +----------+ 9 rows in set (0.00 sec)
Following is the query to implement multiple COUNT() methods in one query −
mysql> select COUNT(CASE WHEN LastName='Smith' THEN 1 END) AS TotalFrequency, -> COUNT(CASE WHEN LastName='Brown' THEN 1 END) AS TotalFrequency, -> COUNT(CASE WHEN LastName='Taylor' THEN 1 END) AS TotalFrequency, -> COUNT(CASE WHEN LastName='Miller' THEN 1 END) AS TotalFrequency -> from DemoTable;
Output
This will produce the following output −
+----------------+----------------+----------------+----------------+ | TotalFrequency | TotalFrequency | TotalFrequency | TotalFrequency | +----------------+----------------+----------------+----------------+ | 2 | 2 | 2 | 3 | +----------------+----------------+----------------+----------------+ 1 row in set (0.00 sec)
- Related Articles
- Implement multiple LIKE operators in a single MySQL query
- Multiple COUNT() for multiple conditions 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?
- MySQL update multiple records in a single query?
- Insert multiple rows in a single MySQL query
- MySQL query to sort multiple columns together in a single query
- Implement SELECT LIKE and CHAR_LENGTH() in a single MySQL query
- Count two different columns in a single query in MySQL?
- How to get multiple rows in a single MySQL query?
- How to obtain multiple rows in a single MySQL query?
- Count boolean field values within a single MySQL query?
- Count and sort rows with a single MySQL query
- MySQL query to count rows in multiple tables
- Use LIKE % to fetch multiple values in a single MySQL query

Advertisements