how to make a query to show the average of the previous day with php and mysql

0

I have a query which shows me the average of the previous day of a column:

    $date = date("Y-m-d");
    $estacion=2;
    $sql="SELECT AVG(TS) as TSprom
    FROM sensores 
    WHERE date<'$date' ";

so far so good, but when I put the id as a condition, it shows me another value from other days, not the day before the current one.

    $date = date("Y-m-d");
    $estacion=2;
    $sql="SELECT AVG(TS) as TSprom1
    FROM sensores 
    WHERE date<'$date' AND idestacion='$estacion' ";

I hope you can help me, thanks

    
asked by Luis Felipe Castro 03.11.2017 в 04:47
source

1 answer

0

In this case you are placing in the condition of the date that is less than the current date, so it brings you the average of all the days less than the current date. You should change the query so that the date is equal to the date of the previous day, directly in Mysql you can do it in the following way:

$sql = "SELECT AVG(TS) as TSprom1
    FROM sensores 
    WHERE date = date(date(now())-1) AND idestacion='$estacion'";

I hope it helps you.

    
answered by 03.11.2017 / 20:04
source