
- 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 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 Articles
- How to select the sum of the column values with higher value in reach row with MySQL?
- MySQL query to select column where value = one or value = two, value = three, etc?
- Show column value twice in MySQL Select?
- 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?
- MySQL SELECT to add a new column to a query and give it a value?
- MySQL select query to fetch data with null value?
- Perform multiplication in SELECT depending on column value in MySQL?
- How to update MySQL column with random value?
- How to select row when column must satisfy multiple value in MySQL?
- Add a temporary column with a value in MySQL?

Advertisements