
- 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
How to add subtotal to a table column displaying NULL in MySQL?
Let us first create a table −
mysql> create table DemoTable -> ( -> Amount int, -> SubTotal int -> ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command −
ysql> insert into DemoTable(Amount) values(50); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Amount) values(60); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Amount) values(70); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Amount) values(80); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+--------+----------+ | Amount | SubTotal | +--------+----------+ | 50 | NULL | | 60 | NULL | | 70 | NULL | | 80 | NULL | +--------+----------+ 4 rows in set (0.00 sec)
Here is the query to add subtotal to a column in MySQL.
mysql> set @sum := 0; Query OK, 0 rows affected (0.10 sec) mysql> update DemoTable set SubTotal = (@sum := @sum + Amount); Query OK, 4 rows affected (0.19 sec) Rows matched: 4 Changed: 4 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
Output
This will produce the following output −
+--------+----------+ | Amount | SubTotal | +--------+----------+ | 50 | 50 | | 60 | 110 | | 70 | 180 | | 80 | 260 | +--------+----------+ 4 rows in set (0.00 sec)
- Related Articles
- How to add a NOT NULL column in MySQL?
- How to Add Percentage of Grand Total/Subtotal Column in an Excel Pivot Table?
- How to add a column in a table in MySQL?
- How to add not null constraint to existing column in MySQL?
- How to add a column to a MySQL table in Python?
- Alter a table column from VARCHAR to NULL in MySQL
- How to add NOT NULL constraint to an already created MySQL column?
- Can we add a column to a table from another table in MySQL?
- How to add a NOT NULL constraint to a column of a table in a database using JDBC API?
- Selecting and displaying only some rows from a column in a MySQL table
- How to add a column using MySQL SELECT in an already created table?
- How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?
- How to add a “created at” column in a table to set the timestamp in MySQL?
- How to add duplicate varchar values without displaying error in MySQL?
- How can we apply a NOT NULL constraint to a column of an existing MySQL table?

Advertisements