-1

In mysql how to get the sum between 2 dates. In my query I'm getting the days between the same months(01-01-2016 to 31-01-2016) But I'm not getting 2 different months (01-01-2016 to 06-02-2016) the result is returns null value.

select sum(cash),sum(advnce) 
from tablename where date 
between '$strt_dte' and '$srch_prev_date' 
GROUP by date
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Syam sg
  • 11
  • 4
  • 1
    MySQL uses YYYY-mm-dd as date format. You're doing the reverse. Plus, we've no way to tell how your variables are being rendered as. You should also use aliases. – Funk Forty Niner Feb 06 '16 at 14:52
  • 1
    Don't group by date if you want a sum between two dates. – xlecoustillier Feb 06 '16 at 14:53
  • 1
    Possible duplicate of [How do I query between two dates using MySQL?](http://stackoverflow.com/questions/3822648/how-do-i-query-between-two-dates-using-mysql) – zoom Feb 06 '16 at 16:39

1 Answers1

0

Give the following a go. Make sure your dates are formatted correctly!

$strt_dte = '2016-01-01';
$srch_prev_date = '2016-01-31';
$sql = "
    SELECT SUM(cash), SUM(advnce)
    FROM tablename
    WHERE date BETWEEN '{$strt_dte}' AND '{$srch_prev_date}'
    GROUP BY date
";
Tom Wright
  • 2,841
  • 4
  • 22
  • 30