MySQL में COUNT(कॉलम) से NULL को छोड़कर गिनती करें
NOT NULL मान की उपस्थिति गिनने के लिए, एग्रीगेट फ़ंक्शन COUNT(yourColumnName) का उपयोग करें। आइए पहले एक टेबल बनाते हैं:
mysql> create table DemoTable
(
Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
NumberOfQuestion int,
NumberOfSolution int
); Query OK, 0 rows affected (0.20 sec)
इन्सर्ट कमांड से टेबल में कुछ रिकॉर्ड डालें। यहां कुछ मान NULL हैं:
mysql> insert into DemoTable(NumberOfQuestion,NumberOfSolution) values(20,10); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(NumberOfQuestion,NumberOfSolution) values(20,2); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(NumberOfQuestion,NumberOfSolution) values(20,NULL); Query OK, 1 row affected (0.03 sec) mysql> insert into DemoTable(NumberOfQuestion,NumberOfSolution) values(20,NULL); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(NumberOfQuestion,NumberOfSolution) values(30,19); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(NumberOfQuestion,NumberOfSolution) values(30,1); Query OK, 1 row affected (0.04 sec)
सेलेक्ट स्टेटमेंट से टेबल के सभी रिकॉर्ड प्रदर्शित करें:
mysql> select *from DemoTable;
आउटपुट:
+----+------------------+------------------+ | Id | NumberOfQuestion | NumberOfSolution | +----+------------------+------------------+ | 1 | 20 | 10 | | 2 | 20 | 2 | | 3 | 20 | NULL | | 4 | 20 | NULL | | 5 | 30 | 19 | | 6 | 30 | 1 | +----+------------------+------------------+ 6 rows in set (0.00 sec)
मान की उपस्थिति गिनने का क्वेरी। वही मान NOT NULL होना चाहिए यानी NULL मान नहीं गिने जाएंगे:
mysql> SELECT NumberOfQuestion, COUNT(NumberOfSolution) as NumberOfRows FROM DemoTable GROUP BY NumberOfQuestion;
आउटपुट:
+------------------+--------------+ | NumberOfQuestion | NumberOfRows | +------------------+--------------+ | 20 | 2 | | 30 | 2 | +------------------+--------------+ 2 rows in set (0.00 sec)
व्याख्या: COUNT(NumberOfSolution) केवल गैर-NULL मान गिनता है। NumberOfQuestion=20 के लिए, 4 पंक्तियाँ हैं लेकिन केवल 2 में गैर-NULL NumberOfSolution है। COUNT(*) सभी पंक्तियाँ गिनता (NULL सहित)।