
- 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 query to find sum of fields with same column value?
Use GROUP BY clause for this. Let us first create a table −
mysql> create table sumOfFieldsDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientSerialNumber varchar(100), -> ClientCost int -> ); Query OK, 0 rows affected (0.50 sec)
Following is the query to insert some records in the table using insert command −
mysql> insert into sumOfFieldsDemo(ClientSerialNumber,ClientCost) values('1111',450); Query OK, 1 row affected (0.16 sec) mysql> insert into sumOfFieldsDemo(ClientSerialNumber,ClientCost) values('2222',550); Query OK, 1 row affected (0.15 sec) mysql> insert into sumOfFieldsDemo(ClientSerialNumber,ClientCost) values('3333',150); Query OK, 1 row affected (0.64 sec) mysql> insert into sumOfFieldsDemo(ClientSerialNumber,ClientCost) values('3333',250); Query OK, 1 row affected (0.12 sec) mysql> insert into sumOfFieldsDemo(ClientSerialNumber,ClientCost) values('2222',1000); Query OK, 1 row affected (0.10 sec) mysql> insert into sumOfFieldsDemo(ClientSerialNumber,ClientCost) values('1111',1000); Query OK, 1 row affected (0.16 sec) mysql> insert into sumOfFieldsDemo(ClientSerialNumber,ClientCost) values('1111',500); Query OK, 1 row affected (0.17 sec) mysql> insert into sumOfFieldsDemo(ClientSerialNumber,ClientCost) values('4444',100); Query OK, 1 row affected (0.17 sec)
Following is the query to display all records from the table using select statement −
mysql> select * from sumOfFieldsDemo;
This will produce the following output −
+----+--------------------+------------+ | Id | ClientSerialNumber | ClientCost | +----+--------------------+------------+ | 1 | 1111 | 450 | | 2 | 2222 | 550 | | 3 | 3333 | 150 | | 4 | 3333 | 250 | | 5 | 2222 | 1000 | | 6 | 1111 | 1000 | | 7 | 1111 | 500 | | 8 | 4444 | 100 | +----+--------------------+------------+ 8 rows in set (0.00 sec)
Here is the query to find sum of fields with same column value −
mysql> select Id,ClientSerialNumber,SUM(ClientCost) AS TotalSum -> from sumOfFieldsDemo -> group by ClientSerialNumber;
This will produce the following output −
+----+--------------------+----------+ | Id | ClientSerialNumber | TotalSum | +----+--------------------+----------+ | 1 | 1111 | 1950 | | 2 | 2222 | 1550 | | 3 | 3333 | 400 | | 8 | 4444 | 100 | +----+--------------------+----------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL query to find same value on multiple fields with LIKE operator?
- MySQL query to get sum of each column where every column has same number of values?
- MySQL SELECT to sum a column value with previous value
- MongoDB query with fields in the same document?
- MongoDB query to sum specific fields
- MySQL query to replace a column value
- MySQL query to find the average of rows with the same ID
- MySQL query to remove a value with only numbers in a column
- MySQL query to replace special characters from column value
- MySQL query to calculate sum from 5 tables with a similar column named “UP”?
- How to use MySQL LIKE query to search a column value with % in it?
- MySQL query to sum 3 different values in a column displaying total of each value in result set?
- Find sum with MySQL SUM() and give aliases for column heading
- MySQL query to sum the Product Price values from similar columns for same customers and display the result in the same column
- Find rows that have the same value on a column in MySQL?

Advertisements