
- 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 the Product Price values from similar columns for same customers and display the result in the same column
For this, use SUM() along with GROUP BY. Let us first create a table −
mysql> create table DemoTable ( CustomerName varchar(100), Product_1_Price int, Product_2_Price int ); Query OK, 0 rows affected (0.73 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John',67,89); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('David',769,890); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David',987,1000); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('John',900,111); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------------+-----------------+-----------------+ | CustomerName | Product_1_Price | Product_2_Price | +--------------+-----------------+-----------------+ | John | 67 | 89 | | David | 769 | 890 | | David | 987 | 1000 | | John | 900 | 111 | +--------------+-----------------+-----------------+ 4 rows in set (0.00 sec)
Following is the query to SUM the values and display the result for the same customers −
mysql> select CustomerName,SUM(Product_1_Price) AS Total1,SUM(Product_2_Price) As Total2 from DemoTable group by CustomerName;
This will produce the following output −
+--------------+--------+--------+ | CustomerName | Total1 | Total2 | +--------------+--------+--------+ | John | 967 | 200 | | David | 1756 | 1890 | +--------------+--------+--------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to group by column and display the sum of similar values in another column
- How to display two different sums of the same price from column Amount in MySQL?
- Query to divide the values of two columns and display the result in a new column using MySQL wildcard?
- How to select different values from same column and display them in different columns with MySQL?
- MySQL query to sum the values of similar columns from two different tables for a particular ID
- MySQL query to count the duplicate ID values and display the result in a separate column
- MySQL query to get the length of all columns and display the result in a single new column?
- MySQL query to merge rows if Id is the same and display the highest corresponding value from other columns
- Display the sum of positive and negative values from a column in separate columns with MySQL
- MySQL query to count occurrences of distinct values and display the result in a new column?
- How to concatenate columns based on corresponding duplicate id values in MySQL? Display the duplicate values in the same column separated by slash
- MySQL query to get sum of each column where every column has same number of values?
- Setting column values as column names in the MySQL query result?
- Select distinct names from two columns in MySQL and display the result in a single column
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?

Advertisements