

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to calculate sum from 5 tables with a similar column named “UP”?
For this, use UNION ALL along with SUM(). Let us create 5 tables −
mysql> create table DemoTable1977 ( UP int ); Query OK, 0 rows affected (0.00 sec) mysql> insert into DemoTable1977 values(10); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1977 values(20); Query OK, 1 row affected (0.00 sec) mysql> select * from DemoTable1977; +------+ | UP | +------+ | 10 | | 20 | +------+ 2 rows in set (0.00 sec) mysql> create table DemoTable1978 ( UP int ); Query OK, 0 rows affected (0.00 sec) mysql> insert into DemoTable1978 values(30); Query OK, 1 row affected (0.00 sec) mysql> select * from DemoTable1978; +------+ | UP | +------+ | 30 | +------+ 1 row in set (0.00 sec) mysql> create table DemoTable1979 ( UP int ); Query OK, 0 rows affected (0.00 sec) mysql> insert into DemoTable1979 values(40); Query OK, 1 row affected (0.00 sec) mysql> select * from DemoTable1979; +------+ | UP | +------+ | 40 | +------+ 1 row in set (0.00 sec) mysql> create table DemoTable1980 ( UP int ); Query OK, 0 rows affected (0.00 sec) mysql> insert into DemoTable1980 values(50); Query OK, 1 row affected (0.00 sec) mysql> select * from DemoTable1980; +------+ | UP | +------+ | 50 | +------+ 1 row in set (0.00 sec) mysql> create table DemoTable1981 ( UP int ); Query OK, 0 rows affected (0.00 sec) mysql> insert into DemoTable1981 values(60); Query OK, 1 row affected (0.00 sec) mysql> select * from DemoTable1981; +------+ | UP | +------+ | 60 | +------+ 1 row in set (0.00 sec)
Here is the query to calculate sum from 5 tables with a single column named “UP” −
mysql> select sum(TotalSum) from ( select sum(UP) as TotalSum from DemoTable1977 union all select sum(UP) from DemoTable1978 union all select sum(UP) from DemoTable1979 union all select sum(UP) from DemoTable1980 union all select sum(UP) from DemoTable1981 ) tbl;
This will produce the following output −
+---------------+ | sum(TotalSum) | +---------------+ | 210 | +---------------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- MySQL query error with a table named “order”?
- MongoDB: How to query a collection named “version”?
- How to query MongoDB similar to “like” ?
- Write a MySQL query equivalent to “SHOW TABLES” in sorted order?
- Only show tables with certain patterns in MySQL “show tables”?
- MySQL query to sum the values of similar columns from two different tables for a particular ID
- MySQL add “prefix” to every column?
- MySQL SELECT from two tables with a single query
- Can we get records “Jone Deo” or “Deo Jone” with a single MySQL query?
- How to query MongoDB with “like”?
- MySQL select only a single value from 5 similar values?
- Create a table named “select” in SQL databases?
- Maintaining order in MySQL “IN” query?
- MySQL query to group by column and display the sum of similar values in another column
- Implement a query similar to MySQL Union with MongoDB?
Advertisements