
- 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 use COUNT() and IF() in a single MySQL query?
Let us first create a table −
mysql> create table DemoTable ( isValidUser boolean ); Query OK, 0 rows affected (0.70 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(true); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values(false); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+ | isValidUser | +-------------+ | 1 | | 0 | | 0 | | 1 | | 1 | | 1 | | 0 | +-------------+ 7 rows in set (0.00 sec)
Let us now see the query to use COUNT() and IF() in a single MySQL query −
mysql> select count(if(isValidUser=1,1,NULL)) AS TOTAL_NUMBER_OF_ONE from DemoTable;
This will produce the following output −
+---------------------+ | TOTAL_NUMBER_OF_ONE | +---------------------+ | 4 | +---------------------+ 1 row in set (0.06 sec)
- Related Articles
- How to use a single MySQL query to count column values ignoring null?
- Count and sort rows with a single MySQL query
- Implement multiple COUNT() in a single MySQL query
- How to count rows from two tables in a single MySQL query?
- How to use count with CASE condition in a MySQL query?
- Count two different columns in a single query in MySQL?
- Can we use WHERE, AND & OR in a single MySQL query?
- Multiple COUNT() for multiple conditions in a single MySQL query?
- Count boolean field values within a single MySQL query?
- Use LIKE % to fetch multiple values in a single MySQL query
- How to add column and index in a single MySQL query?
- Get the count of two table fields in a single MySQL query?
- Get multiple count in a single MySQL query for specific column values
- 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?

Advertisements