MySQL में COUNT(*) फ़ंक्शन का उपयोग करके सटीक पंक्ति गणना
COUNT(*) एग्रीगेट फ़ंक्शन टेबल में सभी पंक्तियों की सटीक संख्या लौटाता है, जिसमें NULL मान वाली पंक्तियाँ भी शामिल हैं।
सिंटैक्स
SELECT COUNT(*) AS anyAliasName FROM yourTableName;
उदाहरण
टेबल बनाएं:
mysql> create table CountAllRowsDemo (
-> Id int,
-> Name varchar(10),
-> Age int
-> );
डेटा डालें (NULL मान सहित):
mysql> insert into CountAllRowsDemo values(1, 'John', 23); mysql> insert into CountAllRowsDemo values(101, 'Carol', 21); mysql> insert into CountAllRowsDemo values(201, 'Sam', 24); mysql> insert into CountAllRowsDemo values(106, 'Mike', 26); mysql> insert into CountAllRowsDemo values(290, 'Bob', 25); mysql> insert into CountAllRowsDemo values(500, 'David', 27); mysql> insert into CountAllRowsDemo values(500, 'David', 27); -- डुप्लिकेट mysql> insert into CountAllRowsDemo values(NULL, NULL, NULL); -- सभी NULL mysql> insert into CountAllRowsDemo values(NULL, NULL, NULL); -- सभी NULL
गिनती करें:
mysql> select count(*) as TotalNumberOfRows from CountAllRowsDemo;
परिणाम:
+-------------------+ | TotalNumberOfRows | +-------------------+ | 9 | +-------------------+
COUNT के प्रकार
| फ़ंक्शन | गिनती |
|---|---|
| COUNT(*) | सभी पंक्तियाँ (NULL सहित) |
| COUNT(column) | गैर-NULL मान वाली पंक्तियाँ |
| COUNT(DISTINCT column) | अद्वितीय गैर-NULL मान |