We can count the number of records in a table with different combinations.
Let us first count the total number of records in the table with this count
SELECT count( * ) as total_record FROM student
Output of above query is here.
This will display total records under the name total_record in the table student. Now we
can add some condition to this SQL to count the records with different
conditions. Let us find out the number of students in class Four in out
We are using alias here to display the value under hading total_record
SELECT count( * ) as total_record FROM `student` WHERE class = 'four'
This will return the count of students in class four only. Here since we are
displaying only one count for a fixed class so we can use this way. We may
require to display total of each class in a table so here we have to use
GROUP BY clause to display totals of each class.
We can count the records in different combinations like finding out the number of students of class four who has got more than 60 mark.
SELECT count(*) FROM `student` WHERE class='Four' and mark >60
SELECT count(*) FROM `student` WHERE class='Four' and mark between 50 and 60
Oupput is here
Count using unique id field
In above query we have used all the columns to find out the total number of records ( by using count(*) ) . In our student table we have id field which is unique and auto incremented. So if we apply count to this id filed then it will more efficient.
SELECT COUNT(id) as total_records FROM student
Counting number of distinct ( unique ) records
SELECT COUNT( DISTINCT class ) FROM student
Output is 7
Records between two date ranges ( PHP Script )
We will keep two variables as $dt1 and $dt2 with two dates. Here date must be in Y-m-d format. Here is the code to change the date format and the query.
$date = new DateTime($dt2);
This query will return the number of records found between two date ranges.
SELECT count(S_NO) as no_tickets FROM main_table WHERE DATE_PL BETWEEN '$dt1' AND '$dt2'
COUNT(if(class='Three',1,NULL)) as THREE,
COUNT(if(class='Four',1,NULL)) as FOUR,
COUNT(if(class='Five',1,NULL)) as FIVE ,
COUNT(if(class='Six',1,NULL)) as SIX,
COUNT(if(class='Seven',1,NULL)) as SEVEN,
COUNT(if(class='Eight',1,NULL)) as EIGHT,
COUNT(if(class='Nine',1,NULL)) as NINIE
Output is here
Number of records by linking two tables
We have two tables, one is storing products (table name product) and other one is storing sales record ( table name sale) . We will get the number of records by linking these two tables.
By using where condition
SELECT product, count(product_sale.product_id) as no FROM `product` , product_sale where product_sale.product_id=product.product_id group by product.product_id
Output is here
In above case there is no information about the products for which there is no record available in our sales table.
By Left join of tables.
SELECT product, count(product_sale.product_id) as no FROM `product` left Join product_sale on product_sale.product_id=product.product_id group by product.product_id
please tell me the query if i want to count the no. employees from a table emp_detail (and display it) and also need to see the different entries of a different table course_detail ? I need to see them on single web page together..
Really simple and nice way to explain... Gr8 gng... Keep it Up ...
u r done a very good job.
i need the answer for how to get the totals of the three columns in the table.
how to get the totals of between two given date.
Pls I need a syntax for count with this scenario: patients who visited their gp in the last three months
how to add a new row of total at the last of all the integer record..
plz help me
Pls i need a syntax to increase the count by 1. (e.g): In a shop bill generation, the bill no should increase automatically by 1 at each bill
hi..plz help me out. i like to get count of records in the table ( included deleted recors ). in other words.. the number of records from the table creation.
is it possible to do this?
Select count(enabled = 1) from user
i have a question. i have a table with 2 fields(name,second field is no of cards)how i calculate all the no of cards with or without using sql,
it is necessary to use sql or not
You have to use SQL , All database supports SQL to manage data.
By using if condition you can create grid view, this part is added to the main contain of this page.