简体   繁体   中英

Select sum of total where the date lies between two dates does not get displayed

I m trying to do a query where it should give me the sum of the column name table, where the timestamp lies between two dates.

Heres my SQL query:

$year_start = strtotime('2018-01-01');
$year_end = strtotime('2018-12-31 23:59:59.000');

$query = "SELECT SUM(total) FROM buchungen WHERE datum>='".$year_start."' AND 
datum<='".$year_end."'";

And then I try to echo the result like so:

while($row = mysqli_fetch_assoc($result)){
echo $row["total"];

A query expects that dates will be in text

$year_start = '2018-01-01';
$year_end = '2018-12-31 23:59:59.000;

$query = "SELECT SUM(total) 
          FROM buchungen 
          WHERE datum>='$year_start' 
          AND datum<='$year_end'";

Also in your test there is no column called total , by default that column would be called SUM(total) unless you rename the column like this

$year_start = '2018-01-01';
$year_end = '2018-12-31 23:59:59.000;

$query = "SELECT SUM(total) as total 
          FROM buchungen 
          WHERE datum>='$year_start' 
          AND datum<='$year_end'";

You must format date to sql format yyyy-mm-dd hh:mm:ss.

Something like

$year_start = date("YYYY-mm-dd", strtotime('2018-01-01'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM