
- 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
Get total in the last row of MySQL result?
To get total in the last row of MySQL result, use the following syntax −
( SELECT yourColumnName1, yourColumnName2, yourColumnName3, . . N FROM yourTableName ) UNION ( SELECT "yourMessage" AS anyAliasName1, SUM(yourColumnName1) AS anyAliasName2, SUM(yourColumnName2) AS anyAliasName3, . . N FROM yourTableName );
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table ProductDemo -> ( -> ProductId varchar(10), -> ProductQuantity int, -> ProductValue int -> ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into ProductDemo values('Product-1',10,300); Query OK, 1 row affected (0.10 sec) mysql> insert into ProductDemo values('Product-2',5,200); Query OK, 1 row affected (0.17 sec) mysql> insert into ProductDemo values('Product-3',7,340); Query OK, 1 row affected (0.13 sec) mysql> insert into ProductDemo values('Product-4',20,500); Query OK, 1 row affected (0.10 sec) mysql> insert into ProductDemo values('Product-5',30,1000); Query OK, 1 row affected (0.42 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from ProductDemo;
The following is the output −
+-----------+-----------------+--------------+ | ProductId | ProductQuantity | ProductValue | +-----------+-----------------+--------------+ | Product-1 | 10 | 300 | | Product-2 | 5 | 200 | | Product-3 | 7 | 340 | | Product-4 | 20 | 500 | | Product-5 | 30 | 1000 | +-----------+-----------------+--------------+ 5 rows in set (0.00 sec)
Here is the query to get total in the last row of MySQL result −
mysql> (SELECT ProductId, -> ProductQuantity, -> ProductValue -> FROM ProductDemo) -> UNION -> (SELECT "Total" AS ProductName, -> SUM(ProductQuantity) AS TotalQuantity, -> SUM(ProductValue) AS TotalValue -> FROM ProductDemo);
Output
+-----------+-----------------+--------------+ | ProductId | ProductQuantity | ProductValue | +-----------+-----------------+--------------+ | Product-1 | 10 | 300 | | Product-2 | 5 | 200 | | Product-3 | 7 | 340 | | Product-4 | 20 | 500 | | Product-5 | 30 | 1000 | | Total | 72 | 2340 | +-----------+-----------------+--------------+ 6 rows in set (0.00 sec)
- Related Articles
- Get the second last row of a table in MySQL?
- How to select first and last data row from a MySQL result?
- Access last inserted row in MySQL?
- In MySQL, how we can get the total value by category in one output row?
- How to select last row in MySQL?
- Display sum in last row of table using MySQL?
- How to get last row in Android sqlite?
- Which is the fastest method to get the total row count for a MySQL Query?
- Get the last days of all the months in MySQL?
- Get last second of a date in MySQL?
- Get the Average of Average in a single MySQL row?
- Generate the row count (serial number) of records after returning the result in MySQL query?
- Get the average row length of a MySQL table
- How to do a sum of previous row value with the current row and display the result in another row with MySQL cross join?
- How to get last day of the current month in MySQL?

Advertisements