
- 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 NOT NULL values from separate tables in a single MySQL query
To count values from separate tables, the syntax is as follows −
Select ( select count(yourColumnName) from yourTableName1) as anyAliasName1, ( select count(yourColumnName) from yourTableName2) as anyAliasName2;
Let us first create a table −
mysql> create table DemoTable1 -> ( -> Id int -> ); Query OK, 0 rows affected (1.06 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(1); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1 values(NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(2); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable1 values(3); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1;
This will produce the following output −
+------+ | Id | +------+ | 1 | | NULL | | 2 | | 3 | +------+ 4 rows in set (0.00 sec)
Following is the query to create second table −
mysql> create table DemoTable2 -> ( -> Id int -> ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values(10); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable2 values(NULL); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2 values(NULL); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable2;
This will produce the following output −
+------+ | Id | +------+ | 10 | | NULL | | NULL | +------+ 3 rows in set (0.00 sec)
Here is the query to get count from separate tables −
mysql> select -> ( -> select count(Id) from DemoTable1) as CountFirstTableId, -> ( -> select count(Id) from DemoTable2) as CountSecondTableId -> ;
This will produce the following output −
+-------------------+--------------------+ | CountFirstTableId | CountSecondTableId | +-------------------+--------------------+ | 3 | 1 | +-------------------+--------------------+ 1 row in set (0.00 sec)
- Related Articles
- Ignore NULL values from separate tables in a single MySQL query and display count of NOT NULL records
- How to count rows from two tables in a single MySQL query?
- Count zero, NULL and distinct values except zero and NULL with a single MySQL query
- How to use a single MySQL query to count column values ignoring null?
- A single query to get the sum of count from different tables in MySQL?
- MySQL SELECT from two tables with a single query
- Count boolean field values within a single MySQL query?
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Get multiple count in a single MySQL query for specific column values
- Implement multiple COUNT() in a single MySQL query
- MySQL query to count rows in multiple tables
- MySQL select and insert in two tables with a single query
- Write a single MySQL query to return the ID from the corresponding row which is a NOT NULL value
- How to count null values in MySQL?

Advertisements