
- 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
How to sum cells in a column if a condition is met in another column with MySQL?
For this, you can use the aggregate function SUM() along with the GROUP BY clause. Let us first create a table −
mysql> create table DemoTable -> ( -> EmployeeName varchar(20), -> JoiningDate date, -> Salary int -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('David','2019-11-02',400); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable values('Robert','2018-11-25',100); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable values('Bob','2019-12-14',600); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('Carol','2019-11-03',300); Query OK, 1 row affected (0.23 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+--------------+-------------+--------+ | EmployeeName | JoiningDate | Salary | +--------------+-------------+--------+ | David | 2019-11-02 | 400 | | Robert | 2018-11-25 | 100 | | Bob | 2019-12-14 | 600 | | Carol | 2019-11-03 | 300 | +--------------+-------------+--------+ 4 rows in set (0.00 sec)
Here is the query to sum cells in a column if a condition is met in another column −
mysql> select year(JoiningDate) as JoiningYear, -> month(JoiningDate) as JoiningMonth, -> sum(Salary) as Total, -> group_concat(EmployeeName) as Name -> from DemoTable -> group by JoiningYear,JoiningMonth;
This will produce the following output −
+-------------+--------------+-------+-------------+ | JoiningYear | JoiningMonth | Total | Name | +-------------+--------------+-------+-------------+ | 2018 | 11 | 100 | Robert | | 2019 | 11 | 700 | David,Carol | | 2019 | 12 | 600 | Bob | +-------------+--------------+-------+-------------+ 3 rows in set (0.04 sec)
- Related Articles
- Select a column if condition is met in MySQL to fetch records from current date and current date + 1
- SUM a column based on a condition in MySQL
- Update one column data to another column in MySQL if the second column is NOT NULL?
- Is it impossible to add a column in MySQL specifically before another column?
- How to find the sum of a column values up to a value in another column in R?
- How to create a Cumulative Sum Column in MySQL?
- How to sum elements of a column in MySQL?
- How to create a new column in an R data frame based on some condition of another column?
- How to find if a column is auto_increment in MySQL?
- How to match a column in a data frame with a column in another data frame in R?
- How to get the sum for every distinct value in another column in MySQL?
- Update a column based on another MySQL table’s column
- MySQL query to group by column and display the sum of similar values in another column
- GROUP BY a column in another MySQL table
- Group by one column and display corresponding records from another column with a separator in MySQL

Advertisements