
- 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 value for multiple columns in MySQL?
To count value for multiple columns, use the CASE statement. Let us first create a table::
mysql> create table countValueMultipleColumnsDemo -> ( -> Value1 int, -> Value2 int, -> Value3 int -> ); Query OK, 0 rows affected (0.62 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into countValueMultipleColumnsDemo values(10,15,10); Query OK, 1 row affected (0.15 sec) mysql> insert into countValueMultipleColumnsDemo values(20,30,10); Query OK, 1 row affected (0.14 sec) mysql> insert into countValueMultipleColumnsDemo values(40,10,60); Query OK, 1 row affected (0.18 sec)
Following is the query to display records from the table using select command:
mysql> select *from countValueMultipleColumnsDemo;
This will produce the following output
+--------+--------+--------+ | Value1 | Value2 | Value3 | +--------+--------+--------+ | 10 | 15 | 10 | | 20 | 30 | 10 | | 40 | 10 | 60 | +--------+--------+--------+ 3 rows in set (0.00 sec)
Following is the query to count value for multiple columns:
mysql> SELECT (SUM(CASE WHEN Value1 = 10 THEN 1 ELSE 0 END) + -> SUM(CASE WHEN Value2 = 10 THEN 1 ELSE 0 END) + -> SUM(CASE WHEN Value3 = 10 THEN 1 ELSE 0 END)) TOTAL_COUNT -> from countValueMultipleColumnsDemo;
This will produce the following output
+-------------+ | TOTAL_COUNT | +-------------+ | 4 | +-------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL multiple COUNT with multiple columns?
- How to check multiple columns for a single value in MySQL?
- How to calculate value from multiple columns in MySQL?
- Set multiple values for custom columns in MySQL?
- Multiple COUNT() for multiple conditions in a single MySQL query?
- MySQL Select Statement DISTINCT for Multiple Columns?
- Searching multiple columns for a row match in MySQL
- MySQL query to get the highest value from a single row with multiple columns\n\n\n\n\n\n\n\n\n\n
- Get the minimum value from a list with multiple columns in MySQL?
- How to count the number of columns having specific value in MySQL?
- How to check for duplicates in MySQL table over multiple columns?
- MySQL filtering by multiple columns?
- How to search multiple columns in MySQL?
- MySQL count(*) from multiple tables?
- Count multiple rows and display the result in different columns (and a single row) with MySQL

Advertisements