How to use UNION to Get Record Count In MySQL
The UNION operation combines rows from both queries. The operator combines the result returned by the individual SELECT query.
Syntax:
SELECT 'table1' table_names, COUNT(*) AS row_count FROM ' table_name'
UNION
SELECT 'table2', COUNT(*) FROM 'table_name';
Example
The query combines and displays the record counts for the ‘article’ and ‘gfg’ tables. It uses UNION to present the results with corresponding table names (‘article’ and ‘gfg’).
SELECT 'article' table_names, COUNT(*) AS row_count FROM article
UNION
SELECT 'gfg', COUNT(*) FROM gfg;
Output:
Explanation: The output provides a unified result of record counts for both the ‘article‘ and ‘gfg‘ tables. Each row displays a table name (‘article’ or ‘gfg’) alongside the respective count of records in that table.
How to Get Record Count for All Tables in MySQL Database
In DBMS, counting records for all tables within a MySQL database is a fundamental requirement. Understanding the size and distribution of data across tables helps optimize database performance and provides insights into data utilization and growth patterns.
Row or record count means how many records are present in the database. Here we will discuss various methods to efficiently retrieve record counts from all tables within a MySQL database.
Contact Us