
- 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
SUM a column based on a condition in MySQL
Let us first create a table −
mysql> create table DemoTable ( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ProductAmount int, CustomerCountryName varchar(10) ); Query OK, 0 rows affected (0.86 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(ProductAmount,CustomerCountryName) values(190,'US'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(ProductAmount,CustomerCountryName) values(200,'UK'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(ProductAmount,CustomerCountryName) values(1500,'AUS'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(ProductAmount,CustomerCountryName) values(2010,'US'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(ProductAmount,CustomerCountryName) values(7890,'UK'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(ProductAmount,CustomerCountryName) values(500,'US'); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------+---------------+---------------------+ | CustomerId | ProductAmount | CustomerCountryName | +------------+---------------+---------------------+ | 1 | 190 | US | | 2 | 200 | UK | | 3 | 1500 | AUS | | 4 | 2010 | US | | 5 | 7890 | UK | | 6 | 500 | US | +------------+---------------+---------------------+ 6 rows in set (0.00 sec)
Following is the query to sum a column based on a condition. Here, you want to sum the records for the customers with a country “US” −
mysql> select SUM(ProductAmount) AS Total_Amount from DemoTable where CustomerCountryName='US';
This will produce the following output −
+--------------+ | Total_Amount | +--------------+ | 2700 | +--------------+ 1 row in set (0.00 sec)
- Related Articles
- ORDER BY records in MySQL based on a condition
- Creating a Pandas dataframe column based on a given condition in Python
- Update a column based on another MySQL table’s column
- MySQL query to update different fields based on a condition?
- Drop rows from the dataframe based on certain condition applied on a column
- Delete only some rows from a table based on a condition in MySQL
- How to sum cells in a column if a condition is met in another column with MySQL?
- Convert a numeric column to binary factor based on a condition in R data frame
- How to create a column with ratio of two columns based on a condition in R?
- Find the sum of a column values based on another numerical column in R.
- How to sum selected column values based on specific month records in MySQL?
- Change string based on a condition - JavaScript
- Find MongoDB records based on a condition?
- How to create a new column in an R data frame based on some condition of another column?
- Replace each value in a column with the largest value based on a condition in R data frame.

Advertisements