
- 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 merge queries in a single MySQL query to get the count of different values in different columns?
Let us first create a table −
mysql> create table DemoTable760 ( ClientId int, ClientId2 int ); Query OK, 0 rows affected (0.79 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable760 values(100,200); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable760 values(100,200); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable760 values(300,400); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable760 values(300,400); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable760 values(100,200); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable760 values(100,200); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable760 values(400,500); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable760;
This will produce the following output -
+----------+-----------+ | ClientId | ClientId2 | +----------+-----------+ | 100 | 200 | | 100 | 200 | | 300 | 400 | | 300 | 400 | | 100 | 200 | | 100 | 200 | | 400 | 500 | +----------+-----------+ 7 rows in set (0.00 sec)
Following is the query to merge queries in a single query to get the count of different values in different columns −
mysql> select sum(ClientId=100) AS ClientId1,sum(ClientId2=200) AS ClientId2 from DemoTable760;
This will produce the following output -
+-----------+-----------+ | ClientId1 | ClientId2 | +-----------+-----------+ | 4 | 4 | +-----------+-----------+ 1 row in set (0.00 sec)
- Related Articles
- Count two different columns in a single query in MySQL?
- A single query to get the sum of count from different tables in MySQL?
- How to get row count of two tables in different databases in a single query?
- A single MySQL query to search multiple words from different column values
- Get the time difference between values in different columns with MySQL
- How to merge two data frames of different length having same values in all columns but at different positions?
- How to combine different columns of a table to yield a single column in query output in PostgreSQL?
- Get multiple count in a single MySQL query for specific column values
- Counting different distinct items in a single MySQL query?
- Count multiple rows and display the result in different columns (and a single row) with MySQL
- MySQL query to sum the values of similar columns from two different tables for a particular ID
- MySQL Query to get count of unique values?
- Comparing two columns in a single MySQL query to get one row?
- How to select different values from same column and display them in different columns with MySQL?
- MySQL query to set different combinations for values in a table?

Advertisements