
- 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
Count values based on conditions and display the result in different columns with MySQL?
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 count values based on conditions −
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
- Count multiple rows and display the result in different columns (and a single row) with MySQL
- Count only null values in two different columns and display in one MySQL select statement?
- How to select different values from same column and display them in different columns with MySQL?
- Select values that meet different conditions on different rows in MySQL?
- Display records from two columns based on comparison in MySQL?
- Count duplicate ids and display the result in a separate column with MySQL
- Count values greater and less than a specific number and display count in separate MySQL columns?
- MySQL query to count the duplicate ID values and display the result in a separate column
- How to subset matrix row values based on different columns?
- MySQL query to count occurrences of distinct values and display the result in a new column?
- Set conditions for columns with values 0 or 1 in MySQL
- Divide numbers from two columns and display result in a new column with MySQL
- Calculate average of column values and display the result with no decimals in MySQL
- Concatenate two values from the same column with different conditions in MySQL
- Replace records based on conditions in MySQL?

Advertisements