简体   繁体   English

MySQL如何获得某一天的最大值?

[英]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) 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. 我得到一个像“ 1”这样的可变数字 ,这意味着它应该是昨天的最高值。

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 谢谢

If your variable holding yesterday is $date_less and $datetime is the variable holding currnet date then your code look like this 如果您昨天持有的变量为$ date_less,而$ datetime是持有当前日期的变量,则您的代码如下所示

$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 有关添加日期的更多信息,您可以看到此链接

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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