The previous Lesson of MySql Lessons Series was about MySql Joins and in this lesson will learn about MySql Count.
The above query will return the total number of rows in a table. Consider the table student shown in the image below:
If we want to count the total number of students in the above student table, we can use MySql Count like this:
The result of the above query is shown in the image below:
You can see in the above image, there is only 1 column (Count(*)) returned by the count query. We can also assign a name to the column returned by the count query in the query by using column alias i.e AS
The result of the above query is shown in the image below:
The count of students in the student table is returned as total in the above query, as specified.
The above query will count only those students from the student table whose marks are 450 or 470. The resultant number of students will be 4 for the above query.
MySql Count
The basic structure of MySql Count Query is given below:
SELECT Count(*) FROM TableName WHERE 1
The above query will return the total number of rows in a table. Consider the table student shown in the image below:
If we want to count the total number of students in the above student table, we can use MySql Count like this:
SELECT Count(*) FROM `student` WHERE 1
The result of the above query is shown in the image below:
You can see in the above image, there is only 1 column (Count(*)) returned by the count query. We can also assign a name to the column returned by the count query in the query by using column alias i.e AS
SELECT Count(*) AS total FROM `student` WHERE 1
The result of the above query is shown in the image below:
The count of students in the student table is returned as total in the above query, as specified.
MySql Count with WHERE Clause
If we do not want to count all the rows of a table but only some specific rows, we will have to use MySql Count with MySql WHERE Clause i.e
SELECT Count(*) AS total FROM `student` WHERE Marks = 450
OR Marks = 470
The above query will count only those students from the student table whose marks are 450 or 470. The resultant number of students will be 4 for the above query.
No comments:
Post a Comment