0

I have a table which holds sensor data:

id, name(varchar), value(float), time(datetime)

Now i need to select the max value of a certain day.

I'm getting a variable number like "1" which means, it should be the highest value of yesterday.

So how can i use this number to get the max value of this day?

I know so far there is this construct:

subdate(CURDATE(), ".$day.")

And also got a query for max value:

SELECT MAX(value) AS value FROM ".$tablename." WHERE sensor_id=? AND value_id=?

But i have problems to combine this with the date...

Hopefully you can help. thx

user3882511
  • 125
  • 1
  • 10

2 Answers2

0

If your variable holding yesterday is $date_less and $datetime is the variable holding currnet date then your code look like this

$datetime   = date("Y-m-d H:i:s");
$date_less  = 1;
"SELECT MAX(value) AS value FROM ".$tablename." WHERE sensor_id=? AND value_id=? and `time` = ".date('Y-m-d H:i:s', strtotime($datetime.' -'.$date_less.' days'));

for more information on adding days in date you can see this link

A.Z. Soft
  • 526
  • 11
  • 12
0

Based on the information in your question (and using your variable names), I think this query will work for you:

$query = "SELECT MAX(value) AS value 
          FROM " . $tablename . "
          WHERE sensor_id=? AND
                value_id=? AND 
                DATE(time)=SUBDATE(CURDATE()," . $days . ")";
Nick
  • 138,499
  • 22
  • 57
  • 95