- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add records from corresponding duplicate values 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 -> ( -> Name varchar(20), -> Value int -> ); Query OK, 0 rows affected (2.08 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris',50); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David',90); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Chris',60); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Bob',100); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David',80); 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 −
+-------+-------+ | Name | Value | +-------+-------+ | Chris | 50 | | David | 90 | | Chris | 60 | | Bob | 100 | | David | 80 | +-------+-------+ 5 rows in set (0.00 sec)
Following is the query to add records from duplicate values in another column −
mysql> select Name,sum(Value) as GrandTotal from DemoTable group by Name;
This will produce the following output −
+-------+------------+ | Name | GrandTotal | +-------+------------+ | Chris | 110 | | David | 170 | | Bob | 100 | +-------+------------+ 3 rows in set (0.03 sec)
- Related Articles
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Group by one column and display corresponding records from another column with a separator in MySQL
- Get minimum value from a column (floating values) with corresponding duplicate ids in MySQL
- SUM corresponding duplicate records in MySQL
- MySQL query to fetch the maximum corresponding value from duplicate column values
- Implement MySQL conditional GROUP BY with NOT IN to filter records from duplicate column values
- Get all the records with two different values in another column with MySQL
- MySQL query to display the count of distinct records from a column with duplicate records
- Find average of corresponding records (Product Price) from duplicate product ids in MYSQL
- Find specific records from a column with comma separated values in MySQL
- Get first date from timestamp in MySQL group by another column with duplicate value
- Fetch specific rows from a MySQL table with duplicate column values (names)?
- Fetch records from interval of past 3 days from current date in MySQL and add the corresponding records
- Add a temporary column in MySQL where the values depend on another column?
- How to concatenate columns based on corresponding duplicate id values in MySQL? Display the duplicate values in the same column separated by slash

Advertisements