
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL SELECT to sum a column value with previous value
For this, you can use a session variable. Let us first create a table −
mysql> create table DemoTable809(Price int); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable809 values(40); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable809 values(50); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable809 values(60); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable809;
This will produce the following output −
+-------+ | Price | +-------+ | 40 | | 50 | | 60 | +-------+ 3 rows in set (0.00 sec)
Following is the query to sum a column value with a previous value −
mysql> select @currentSum:=@currentSum+Price from DemoTable809;
This will produce the following output −
+--------------------------------+ | @currentSum:=@currentSum+Price | +--------------------------------+ | 40 | | 90 | | 150 | +--------------------------------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- Show column value twice in MySQL Select?
- MySQL query to select column where value = one or value = two, value = three, etc?
- How to select the sum of the column values with higher value in reach row with MySQL?
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- MySQL query to find sum of fields with same column value?
- How to select the maximum value of a column in MySQL?
- Select a specific value between two column values in MySQL?
- How to select distinct value from one MySQL column only?
- How to replace zero with previous value in an R data frame column?
- Add a temporary column with a value in MySQL?
- MySQL select query to fetch data with null value?
- How to update MySQL column with random value?
- MySQL query to replace a column value
- MySQL select count by value?
- MySQL select accumulated (running sum) column?
Advertisements