
- 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 multiple counting without using MySQL COUNT()?
To count, you can use SUM() along with CASE statement for conditions. Let us first create a table −
mysql> create table DemoTable1485 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20), -> StudentSubject varchar(20) -> ); Query OK, 0 rows affected (0.72 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1485(StudentName,StudentSubject) values('Chris','MySQL'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1485(StudentName,StudentSubject) values('Robert','MongoDB'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1485(StudentName,StudentSubject) values('Robert','MongoDB'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1485(StudentName,StudentSubject) values('Chris','Java'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1485;
This will produce the following output −
+-----------+-------------+----------------+ | StudentId | StudentName | StudentSubject | +-----------+-------------+----------------+ | 1 | Chris | MySQL | | 2 | Robert | MongoDB | | 3 | Robert | MongoDB | | 4 | Chris | Java | +-----------+-------------+----------------+ 4 rows in set (0.00 sec)
Here is the query to perform multiple counting without using COUNT() method −
mysql> select StudentSubject, -> sum(case when StudentName = 'Chris' THEN 1 ELSE 0 END) Chris_Count, -> sum(case when StudentName = 'Robert' THEN 1 ELSE 0 END) Robert_Count -> from DemoTable1485 -> group by StudentSubject;
This will produce the following output −
+----------------+-------------+--------------+ | StudentSubject | Chris_Count | Robert_Count | +----------------+-------------+--------------+ | MySQL | 1 | 0 | | MongoDB | 0 | 2 | | Java | 1 | 0 | +----------------+-------------+--------------+ 3 rows in set (0.00 sec)
- Related Articles
- How to perform SELECT using COUNT in MySQL?
- MySQL multiple COUNT with multiple columns?
- MySQL count(*) from multiple tables?
- Perform count with CASE WHEN statement in MySQL?
- Multiple COUNT() for multiple conditions in a single MySQL query?
- Count(*) rows from multiple tables in MySQL?
- How to get number of rows in a table without using count(*) MySQL query?
- Count value for multiple columns in MySQL?\n
- Count multiple occurrences of separate texts in MySQL?
- Implement multiple COUNT() in a single MySQL query
- MySQL query to count rows in multiple tables
- Perform multiple inserts with INSERT INTO SELECT and UNION in MySQL
- How to perform square root without using math module in Python?
- Perform complex MySQL insert by using CONCAT()?
- Perform case insensitive SELECT using MySQL IN()?

Advertisements