
- 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 sum rows having repeated corresponding Id
To sum, you can use aggregate function SUM(). Let us first create a table −
mysql> create table DemoTable850( Id int, Price int ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable850 values(1,90); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable850 values(2,100); Query OK, 1 row affected (0.55 sec) mysql> insert into DemoTable850 values(2,50); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable850 values(1,80); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable850 values(1,60); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable850;
This will produce the following output −
+------+-------+ | Id | Price | +------+-------+ | 1 | 90 | | 2 | 100 | | 2 | 50 | | 1 | 80 | | 1 | 60 | +------+-------+ 5 rows in set (0.00 sec)
Following is the query to sum rows having repeated values −
mysql> select Id,SUM(Price) from DemoTable850 where Id=1;
This will produce the following output −
+------+------------+ | Id | SUM(Price) | +------+------------+ | 1 | 230 | +------+------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL - SUM rows with same ID?
- A single MySQL query to combine strings from many rows into a single row and display the corresponding User Id sum in another column?
- MySQL query to merge rows if Id is the same and display the highest corresponding value from other columns
- MySQL query to return TRUE for rows having positive value?
- MySQL query to find the average of rows with the same ID
- MySQL query to find all rows where ID is divisible by 4?
- MySQL query to display only the column values with corresponding column having whitespace
- MySQL query to select a random row value (Id and Name) having multiple occurrences (Name)?
- MySQL query to sum up values of rows and sort the result?
- MySQL query to group concat distinct by Id?
- MySQL query to select distinct order by id
- Write a single MySQL query to return the ID from the corresponding row which is a NOT NULL value
- MySQL query to select too many rows?
- MySQL query to select multiple rows effectively?
- SUM corresponding duplicate records in MySQL

Advertisements