- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to select a count on two separate conditions?
Use CASE statement for this. Let us first create a table −
mysql> create table DemoTable -> ( -> StudentMarks int, -> isValid tinyint(1) -> ); Query OK, 0 rows affected (0.68 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(45,0); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(78,1); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(45,1); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable values(78,1); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(45,0); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(82,1); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(62,1); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+--------------+---------+ | StudentMarks | isValid | +--------------+---------+ | 45 | 0 | | 78 | 1 | | 45 | 1 | | 78 | 1 | | 45 | 0 | | 82 | 1 | | 62 | 1 | +--------------+---------+ 7 rows in set (0.00 sec)
Here is the query to select a count on two separate conditions −
mysql> select StudentMarks, -> sum(case -> when StudentMarks=45 -> then case when isValid = 1 then 1 else 0 end -> else 1 end -> ) AS Freq -> from DemoTable -> group by StudentMarks;
Output
+--------------+------+ | StudentMarks | Freq | +--------------+------+ | 45 | 1 | | 78 | 2 | | 82 | 1 | | 62 | 1 | +--------------+------+ 4 rows in set (0.00 sec)
- Related Articles
- How to order and select query with conditions in MySQL?
- A single MySQL select query on two tables is possible?
- MySQL query with two boolean conditions to extract date based on hour?
- Multiple COUNT() for multiple conditions in a single MySQL query?
- Count NOT NULL values from separate tables in a single MySQL query
- MySQL query to select a record with two exact values?
- How to do a count on a MySQL union query?
- MySQL SELECT from two tables with a single query
- Select multiple sums with MySQL query and display them in separate columns?
- MySQL query to count the duplicate ID values and display the result in a separate column
- MySQL query to separate and select string values (with hyphen) from one column to different columns
- Select values that meet different conditions on different rows in MySQL?
- MongoDB query to select distinct and count?
- Count two different columns in a single query in MySQL?
- How to count rows from two tables in a single MySQL query?

Advertisements