Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

MySQL How to sum rows between a range of Timestamps

MySQL How to sum rows between a range of Timestamps

Sometime we have to face a problem liking summing a MySQL field value between given two timestamps, and
we have to rely on google, stack overflow for answer but sometime it become difficult to get desire result.
In this post I will go through a example to solve our problem How to sum rows over a range of timestamps.

Suppose we have a table called courses (Database Name: institute) that contain a fields …

  • id(11) auto_increment primary key
  • coursename varchar(100)
  • duration varchar(20)
  • coursefee decimal(10,2)
  • discount decimal(10,2)
  • netcoursefee decimal(10,2)
  • idate (timestamp)

We have to find total discount given to student between 2019-01-01 to 2019-03-27

<?php
//database connection
$db= new mysqli('localhost','root','','institute');
$sql = "SELECT SUM(discount) as total FROM courses WHERE DATE(idate) BETWEEN '2019-01-01' AND '2019-03-27'" ;
//var_dump($sql);
$query=$db->query($sql) or die($db->error);
$row=$query->fetch_assoc();
$disc= $row['total'];
?>
Discount Given : <?php echo $disc;?>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do you have any Query / Suggestion