
- 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
How to count rows from two tables in a single MySQL query?
Let us first create a table −
mysql> create table DemoTable1 ( Name varchar(40) ); Query OK, 0 rows affected (0.81 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values('Chris'); Query OK, 1 row affected (0.48 sec) mysql> insert into DemoTable1 values('Robert'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1 values('Mike'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+--------+ | Name | +--------+ | Chris | | Robert | | Mike | +--------+ 3 rows in set (0.00 sec)
Following is the query to create the second table −
mysql> create table DemoTable2 ( Score int ); Query OK, 0 rows affected (1.16 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values(86); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable2 values(98); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable2 values(75); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2;
This will produce the following output −
+-------+ | Score | +-------+ | 86 | | 98 | | 75 | +-------+ 3 rows in set (0.00 sec)
Following is the query to count rows from two tables in one query −
mysql> select (select count(*) from DemoTable1) AS FirstTableCount, (select count(*) from DemoTable2) AS SecondTableCount from dual;
This will produce the following output −
+-----------------+------------------+ | FirstTableCount | SecondTableCount | +-----------------+------------------+ | 3 | 3 | +-----------------+------------------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to count rows in multiple tables
- MySQL SELECT from two tables with a single query
- Count NOT NULL values from separate tables in a single MySQL query
- A single query to get the sum of count from different tables in MySQL?
- How to insert into two tables using a single MySQL query?
- Count(*) rows from multiple tables in MySQL?
- Count and sort rows with a single MySQL query
- How to get row count of two tables in different databases in a single query?
- A single MySQL select query on two tables is possible?
- MySQL select and insert in two tables with a single query
- Count two different columns in a single query in MySQL?
- How to get multiple rows in a single MySQL query?
- How to obtain multiple rows in a single MySQL query?
- A single MySQL query to find the highest and lowest among two tables?
- Ignore NULL values from separate tables in a single MySQL query and display count of NOT NULL records

Advertisements