If you want to get the count of values in a column, it’s pretty much simple to get the required result by executing a simple query. Same applies if you want the total count of rows in a table. You just use COUNT() function of MySQL.
If your table has a column which holds different values for different rows then also you can make use of the same COUNT() function to find the counts of each. This time, you use
COUNT() function along with the GROUP BY clause of MySQL.
Consider the following example table holding the details of students’ promotion status:
id(PK) | class_id(FK) | student_id(FK) | status |
1 | 1 | 1 | Passed |
2 | 1 | 2 | Failed |
3 | 1 | 3 | Passed |
4 | 2 | 4 | Passed |
5 | 2 | 5 | Failed |
6 | 3 | 6 | Passed |
To get the count of students for each status, execute the following query:
SELECT status,COUNT(id) FROM student_certificate where class_id=1 GROUP BY status
You will get the result as following:
status | count |
Passed | 2 |
Failed | 1 |