user3882511
user3882511

Reputation: 125

mysql how to get max value for a certain day?

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

Upvotes: 0

Views: 227

Answers (2)

Nick
Nick

Reputation: 147206

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 . ")";

Upvotes: 0

A.Z. Soft
A.Z. Soft

Reputation: 546

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

Upvotes: 0

Related Questions