For this, you can use an aggregate function SUM() along with the condition. Let us first create a table −
mysql> create table DemoTable -> ( -> Status varchar(20) -> ); Query OK, 0 rows affected (1.22 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('active'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('active'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('active'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values('active'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | Status | +--------+ | active | | active | | active | | active | +--------+ 4 rows in set (0.00 sec)
Here is the query to display row with zero value −
mysql> select -> sum(Status='active') as 'CountOfActive', -> sum(Status='inactive') as 'CountOfInActive' -> from DemoTable;
This will produce the following output. Here, for status inactive none of the records are in the table, therefore 0 would be visible −
+---------------+-----------------+ | CountOfActive | CountOfInActive | +---------------+-----------------+ | 4 | 0 | +---------------+-----------------+ 1 row in set (0.30 sec)