
- 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
MySQL select to count values equal to 0 and greater than 0 from a column?
For this, use the CASE statement. Let us first create a table −
mysql> create table DemoTable ( Number int ); Query OK, 0 rows affected (0.83 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(50); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+--------+ | Number | +--------+ | 10 | | 0 | | 20 | | 50 | | 0 | | 0 | | 40 | +--------+ 7 rows in set (0.00 sec)
Let us now see how to select count values equal to 0 and greater than 0 from a column
mysql> select sum(case when Number > 0 then 1 else 0 end ) AS TotalValueWhichIsGreaterThanZero, sum(case when Number = 0 then 1 else 0 end) AS TotalValueWhichIsEqualToZero from DemoTable where Number >=0;
Output
+----------------------------------+------------------------------+ | TotalValueWhichIsGreaterThanZero | TotalValueWhichIsEqualToZero | +----------------------------------+------------------------------+ | 4 | 3 | +----------------------------------+------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Set the NULL values to 0 and display the entire column in a new column with MySQL SELECT
- How to select values less than or greater than a specific percentile from an R data frame column?
- How to change the color of bars in histogram for values that are greater than 0 or less than 0 in R?
- Count values greater and less than a specific number and display count in separate MySQL columns?
- MySQL randomly select 2 values from column values?
- MySQL query to select rows where column value is only 0, group by another column?
- Select equal or nearest greater number from table in MySQL
- Set all values in a MySQL field to 0?
- How to select only non - numeric values from varchar column in MySQL?
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- MySQL query to separate and select string values (with hyphen) from one column to different columns
- Select current time with MySQL now() and convert it to GMT 0?
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Select distinct values from three columns and display in a single column with MySQL

Advertisements