根据数组选择不同的MAX(值)

时间:2017-03-09 20:34:34

标签: php database sqlite

我想选择不同sensornodes的不同MAX(值)。我的问题是WHERE ... IN ...子句像许多逻辑" OR"一样,但我需要来自每个sensornode的MAX($ measure)。我知道如何用循环来做,但我认为有一个更好的解决方案来做到这一点。

这是表'测量'数据库:

ID          humidity    temperature  date        time        sensornode
----------  ----------  -----------  ----------  ----------  ----------
1           22.00%      18.00C       06/03/2017  13:07:18    WSN1      
2           22.00%      18.00C       06/03/2017  13:08:19    WSN2      
3           34.00%      21.00C       06/03/2017  13:09:19    WSN3      
4           21.00%      20.00C       06/03/2017  13:10:19    WSN4

查询应该是这样的

$measure //is either 'temperature' or 'humidity', depends on the users input.
$sensornode // is a string which is converted with the implode-function from an array which includes the selected 'sensornodes' of the users input
$sql_query = "SELECT MAX($measure) AS $measure 
    FROM measurement 
    WHERE sensornode IN ('$sensornode')"; 
$data = executeQuery($sql_query, $measure);

1 个答案:

答案 0 :(得分:0)

要获取每个传感器节点的最大值,请使用分组:

Program Files\Vim\vim80\lang

要将其限制为某些传感器节点,只需添加WHERE:

SELECT sensornode,
       max(temperature)
FROM measurement
GROUP BY sensornode;