- Trending Categories
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
Counting the total and true condition values in a MySQL table?
For this, you can use COUNT(). Let us first create a table −
mysql> create table DemoTable ( Value int ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 10 | | NULL | | 20 | | 40 | +-------+ 4 rows in set (0.00 sec)
Following is the query to count the total and true condition values in a MySQL table −
mysql> select count(*) as TOTAL_COUNT,count(Value OR NULL) as CONDITION_TRUE from DemoTable;
This will produce the following output −
+-------------+----------------+ | TOTAL_COUNT | CONDITION_TRUE | +-------------+----------------+ | 4 | 3 | +-------------+----------------+ 1 row in set (0.00 sec)
Advertisements