
- 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
Count(*) rows from multiple tables in MySQL?
To count rows from multiple tables in MySQL, the syntax is as follows −
Select (select count(*) from yourTableName1) as anyAliasName1, (select count(*) from yourTableName2) as anyAliasName2 from dual;
Let us first create a table −
mysql> create table DemoTable1 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY -> ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(),(),(),(),(),(); Query OK, 6 rows affected (0.24 sec) Records: 6 Duplicates: 0 Warnings: 0
Display all records from the table using select statement −
mysql> select * from DemoTable1;
This will produce the following output −
+----+ | Id | +----+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | +----+ 6 rows in set (0.00 sec)
Following is the query to create second table −
mysql> create table DemoTable2 -> ( -> Name varchar(20) -> ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2 values('David'); Query OK, 1 row affected (0.31 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable2;
This will produce the following output −
+-------+ | Name | +-------+ | Chris | | David | +-------+ 2 rows in set (0.00 sec)
Following is the query to count rows from multiple tables −
mysql> select -> (select count(*) from DemoTable1) as FirstTable1Count, -> (select count(*) from DemoTable2) as SecondTable2Count -> from dual;
This will produce the following output −
+---------------------+----------------------+ | FirstTable1Count | SecondTable2Count | +---------------------+----------------------+ | 6 | 2 | +---------------------+----------------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to count rows in multiple tables
- MySQL count(*) from multiple tables?
- How to count rows from two tables in a single MySQL query?
- Insert records from multiple tables in MySQL
- Finding total number of rows of tables across multiple databases in MySQL?
- Count from two tables and give combined count of string in MySQL?
- Concatenate data from multiple rows using GROUP_CONCAT() in MySQL?
- Inserting multiple rows in MySQL?
- How to lock multiple tables in MySQL?
- Count NOT NULL values from separate tables in a single MySQL query
- MySQL multiple COUNT with multiple columns?
- How can we delete multiple rows from a MySQL table?
- Finding the sum of integers from multiple MySQL rows in same column?
- Display multiple selected rows using MySQL IN()
- How can we create a MySQL view by using data from multiple tables?

Advertisements