- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Show row with zero value after addition in MySQL?
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)
Advertisements
To Continue Learning Please Login
Login with Google