
- 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
MySQL multiple COUNT with multiple columns?
You can use an aggregate function SUM() along with IF(). Let us first create a table −
mysql> create table DemoTable ( FirstName varchar(100), LastName varchar(100) ); Query OK, 0 rows affected (2.80 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Adam','Smith'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('John','Smith'); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable values('John','Doe'); Query OK, 1 row affected (1.38 sec) mysql> insert into DemoTable values('Bob','Doe'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Sam','Smith'); Query OK, 1 row affected (0.25 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Adam | Smith | | John | Smith | | John | Doe | | Bob | Doe | | Sam | Smith | +-----------+----------+ 5 rows in set (0.00 sec)
Here is the query to MySQL multiple COUNT with multiple columns.
mysql> select sum(if(FirstName='John',1,0)) as John_Count, sum(if(LastName='Smith',1,0)) as Smith_Count from DemoTable;
This will produce the following output −
+------------+-------------+ | John_Count | Smith_Count | +------------+-------------+ | 2 | 3 | +------------+-------------+ 1 row in set (0.00 sec)
- Related Articles
- Count value for multiple columns in MySQL?\n
- MySQL filtering by multiple columns?
- MySQL count(*) from multiple tables?
- Multiple COUNT() for multiple conditions in a single MySQL query?
- Count multiple rows and display the result in different columns (and a single row) with MySQL
- MySQL Select Statement DISTINCT for Multiple Columns?
- MySQL query to GROUP BY multiple columns
- How to search multiple columns in MySQL?
- How to create conditions in a MySQL table with multiple columns?
- Concatenate multiple rows and columns in a single row with MySQL
- Perform multiple counting without using MySQL COUNT()?
- Count(*) rows from multiple tables in MySQL?
- MySQL query to display ranks of multiple columns?
- Update multiple columns of a single row MySQL?
- Change multiple columns in a single MySQL query?

Advertisements