
- 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
Display sum in last row of table using MySQL?
To display sum in last row of a table, you can use UNION. To understand how, let us create a table
mysql> create table showSumInLastRowDemo -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20), -> StudentMarks int -> ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('John',56); Query OK, 1 row affected (0.14 sec) mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('John',87); Query OK, 1 row affected (0.10 sec) mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('John',52); Query OK, 1 row affected (0.17 sec) mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('Carol',97); Query OK, 1 row affected (0.12 sec) mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('Larry',75); Query OK, 1 row affected (0.14 sec) mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('Larry',98); Query OK, 1 row affected (0.10 sec) mysql> insert into showSumInLastRowDemo(StudentName,StudentMarks) values('Carol',73); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from showSumInLastRowDemo;
The following is the output
+-----------+-------------+--------------+ | StudentId | StudentName | StudentMarks | +-----------+-------------+--------------+ | 1 | John | 56 | | 2 | John | 87 | | 3 | John | 52 | | 4 | Carol | 97 | | 5 | Larry | 75 | | 6 | Larry | 98 | | 7 | Carol | 73 | +-----------+-------------+--------------+ 7 rows in set (0.00 sec)
Here is the query to display sum in last row of table using MySQL
mysql> (select StudentName,StudentMarks from showSumInLastRowDemo) -> UNION -> (select 'TotalMarksOfAllStudent' as StudentName,sum(StudentMarks) StudentMarks from showSumInLastRowDemo);
The following is the output
+------------------------+--------------+ | StudentName | StudentMarks | +------------------------+--------------+ | John | 56 | | John | 87 | | John | 52 | | Carol | 97 | | Larry | 75 | | Larry | 98 | | Carol | 73 | | TotalMarksOfAllStudent | 538 | +------------------------+--------------+ 8 rows in set (0.00 sec)
- Related Articles
- Get the second last row of a table in MySQL?
- Display random row from a MySQL table
- Display MySQL table values using Java
- Access last inserted row in MySQL?
- How to select last row in MySQL?
- Get total in the last row of MySQL result?
- Display first selected row in MySQL?
- How to select first and last row record using LIMIT in MySQL?
- Get the sum of multiple row (not all) values from 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?
- Select rows from a MySQL table and display using IN()
- Update the table by calculating the sum and display the result as last column value
- Sum values of a single row in MySQL?
- Why does div display: table-row ignore margin?
- Display all fields of a table in MySQL?

Advertisements