---
title: "mySQL union with calculated row"  
description: "mySQL union with calculated row"  
author: "Dag Hammarskjold"  
published: 2013-07-09  
updated: 2013-07-09  
canonical: https://www.mindstick.com/forum/1277/mysql-union-with-calculated-row  
category: "mysql"  
tags: ["mysql"]  
reading_time: 1 minute  

---

# mySQL union with calculated row

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances)!\
I have a query similar to this:\
[select](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance) 'table_1', count(*)from table_1union select 'table_2', count(*)from table_2union select 'table_n', count(*)from table_n[returning](https://www.mindstick.com/news/2477/returning-ceo-bob-iger-dismisses-the-disney-apple-sale-as-pure-speculation) the total of rows for each table (n [tables](https://www.mindstick.com/articles/336597/introduction-of-html-tables-for-web-development)).\
table_1 | 100 table_2 | 150 table_n | 400I want to know if there is a [mysql](https://www.mindstick.com/articles/12156/what-is-mysql) [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) that can just add a new record at the end making the sum of all rows like this:\
table_1 | 100 table_2 | 150 table_n | 400 total | 650Is there a way to do that in mySQL ([version](https://www.mindstick.com/articles/12845/things-to-remember-while-migrating-odoo-to-a-better-version) 5.5) whithout using a [procedure](https://www.mindstick.com/forum/32/stored-procedure-return-datatype)? (for exemple using a [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) inside the sql if supported)\
Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)! \

## Replies

### Reply by AVADHESH PATEL

Hi,\
Try this\
select ifnull(table_name,'Total'), sum(row_count) from (select 'table_1' table_name, count(*) row_count from table_1 union select 'table_2' table_name, count(*) row_count from table_2 union select 'table_n' table_name, count(*) row_count from table_n ) temp group by table_name with rollup;\
I hope it resolve your problem.


---

Original Source: https://www.mindstick.com/forum/1277/mysql-union-with-calculated-row

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
