
- 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
Perform count with CASE WHEN statement in MySQL?
For this, you can use CASE WHEN statement. Let us first create a table −
mysql> create table DemoTable1910 ( FirstName varchar(20), Marks int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1910 values('Chris',45); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1910 values('David',85); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1910 values('Chris',55); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1910 values('Chris',98); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1910 values('David',98); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1910;
This will produce the following output −
+-----------+-------+ | FirstName | Marks | +-----------+-------+ | Chris | 45 | | David | 85 | | Chris | 55 | | Chris | 98 | | David | 98 | +-----------+-------+ 5 rows in set (0.00 sec)
Here is the query to count column values efficiently −
mysql> select FirstName, sum(case when Marks=45 then 1 else 0 end), sum(case when Marks=98 then 1 else 0 end) from DemoTable1910 group by FirstName;
This will produce the following output −
+-----------+-------------------------------------------+-------------------------------------------+ | FirstName | sum(case when Marks=45 then 1 else 0 end) | sum(case when Marks=98 then 1 else 0 end) | +-----------+-------------------------------------------+-------------------------------------------+ | Chris | 1 | 1 | | David | 0 | 1 | +-----------+-------------------------------------------+-------------------------------------------+ 2 rows in set (0.00 sec)
- Related Articles
- Implement MySQL CASE statement with WHEN clause
- MySQL ORDER BY with CASE WHEN
- Implement CASE statement with WHEN clause to check for the existence of a record in MySQL
- Perform case insensitive SELECT using MySQL IN()?
- MySQL case statement inside a select statement?
- MySQL Mass Update with CASE WHEN/ THEN/ ELSE?
- Alternative to MySQL CASE WHEN in MySQL
- How to use count with CASE condition in a MySQL query?
- In which conditions, MySQL CASE statement return NULL?
- How to perform SELECT using COUNT in MySQL?
- How MySQL can perform case-sensitive string comparison?
- Why should we use MySQL CASE Statement?
- MySQL order by field using CASE Statement
- Perform multiple counting without using MySQL COUNT()?
- Using Prepared statement correctly with WHERE condition in case of any value in MySQL Java

Advertisements