
- 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
MySQL count(*) from multiple tables?
To achieve this for multiple tables, use the UNION ALL.
The syntax is as follows
select sum(variableName.aliasName) from ( select count(*) as yourAliasName from yourTableName1 UNION ALL select count(*) as yourAliasName from yourTableName2 ) yourVariableName;
Let us implement the above syntax. Here, I am using the sample database which has more tables.
The two tables we are using are
- userdemo
- wheredemo
Here is the query to display all records of both the tables. The query is as follows to display records from table ‘userdemo’.
mysql> select *from userdemo;
The following is the output
+--------+----------+------------------+ | UserId | UserName | RegisteredCourse | +--------+----------+------------------+ | 1 | John | Java | | 2 | Larry | C | | 3 | Carol | C++ | | 4 | Mike | C# | +--------+----------+------------------+ 4 rows in set (0.08 sec)
The query is as follows to display records from table ‘wheredemo’.
mysql> select *from wheredemo;
The following is the output
+------+---------+ | Id | Name | +------+---------+ | 101 | Maxwell | | 110 | David | | 1000 | Carol | | 1100 | Bob | | 115 | Sam | +------+---------+ 5 rows in set (0.20 sec)
Here is the query to implement count(*) from both the above tables
mysql> select sum(tbl.EachTableCount) -> from -> ( -> select count(*) as EachTableCount from userdemo -> UNION ALL -> select count(*) as EachTableCount from wheredemo -> )tbl;
The following is the output
+-------------------------+ | sum(tbl.EachTableCount) | +-------------------------+ | 9 | +-------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Count(*) rows from multiple tables in MySQL?
- MySQL query to count rows in multiple tables
- Insert records from multiple tables in MySQL
- Count from two tables and give combined count of string in MySQL?
- MySQL multiple COUNT with multiple columns?
- How to lock multiple tables in MySQL?
- 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 can we create a MySQL view by using data from multiple tables?
- How can I get the output of multiple MySQL tables from a single query?
- A single query to get the sum of count from different tables in MySQL?
- Get record count for all tables in MySQL database?
- How can I sum columns across multiple tables in MySQL?
- Multiple COUNT() for multiple conditions in a single MySQL query?
- Perform multiple counting without using MySQL COUNT()?

Advertisements