
- 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
A single query to get the sum of count from different tables in MySQL?
To get the sum of count from different tables, use UNION ALL. Let us first create a table −
mysql> create table DemoTable1 -> ( -> Id int, -> Name varchar(30) -> ); Query OK, 0 rows affected (1.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(10,'Chris Brown'); Query OK, 1 row affected (0.83 sec) mysql> insert into DemoTable1 values(20,'David Miller'); Query OK, 1 row affected (0.50 sec) mysql> insert into DemoTable1 values(30,'John Adam'); Query OK, 1 row affected (0.83 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
Output
+------+--------------+ | Id | Name | +------+--------------+ | 10 | Chris Brown | | 20 | David Miller | | 30 | John Adam | +------+--------------+ 3 rows in set (0.00 sec)
Following is the query to create second table −
mysql> create table DemoTable2 -> ( -> Amount int -> ); Query OK, 0 rows affected (1.17 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values(100); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable2 values(200); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2 values(300); Query OK, 1 row affected (0.54 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2;
Output
+--------+ | Amount | +--------+ | 100 | | 200 | | 300 | +--------+ 3 rows in set (0.00 sec)
Here is how to get sum of count from different tables in a single query −
mysql> select sum(AllCount) AS Total_Count -> from -> ( -> (select count(*) AS AllCount from DemoTable1) -> union all -> (select count(*) AS AllCount from DemoTable2) -> )t;
Output
+-------------+ | Total_Count | +-------------+ | 6 | +-------------+ 1 row in set (0.03 sec)
- Related Articles
- How to get row count of two tables in different databases in a single query?
- How to count rows from two tables in a single MySQL query?
- Count NOT NULL values from separate tables in a single MySQL query
- How to merge queries in a single MySQL query to get the count of different values in different columns?
- How can I get the output of multiple MySQL tables from a single query?
- Count two different columns in a single query in MySQL?
- MySQL SELECT from two tables with a single query
- MySQL query to sum the values of similar columns from two different tables for a particular ID
- Get the count of two table fields in a single MySQL query?
- Ignore NULL values from separate tables in a single MySQL query and display count of NOT NULL records
- MySQL query to count rows in multiple tables
- Get the count of duplicate values from a single column in MySQL?
- Get multiple count in a single MySQL query for specific column values
- Implement multiple COUNT() in a single MySQL query
- A single MySQL query to search multiple words from different column values

Advertisements