

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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?
- Counting different distinct items in a single MySQL query?
- How to combine different columns of a table to yield a single column in query output in PostgreSQL?
- 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?
- Get multiple count in a single MySQL query for specific column values
- Count multiple rows and display the result in different columns (and a single row) with MySQL
- MySQL query to set different combinations for values in a table?
- How to select different values from same column and display them in different columns with MySQL?
- Count values based on conditions and display the result in different columns with MySQL?
Advertisements