SQL查询与日期不同的最大值

时间:2017-04-23 11:20:01

标签: php sql database sqlite

我的表

ID | sensornode | temperature | humidity | pressure | voltage | datetime
 1           Node1       27.81       23.78        1029.7      3.321       2017-04-20 14:17:24
 2           Node1       27.93       23.72        1029.7      3.321     2017-04-20 14:17:39
 3           Node1       28.1        23.68        1029.8      3.321    2017-04-20 14:17:54
 4           Node1       28.08       23.62        1029.7      3.321     2017-04-20 14:18:09
 5           Node1       28.21       23.58        1029.8      3.321    2017-04-20 14:18:24
 6           Node1       28.37       23.54        1029.7      3.321     2017-04-20 14:18:39

我需要选择Max&最小温度,湿度和压力值。这是选择max和min的PHP方法

public function getMaxMin($nodeId, $datetimestart, $datetimeend){
        $values = array(
            ':nodeId'        => $nodeId,
            ':datetimestart' => $datetimestart, 
            ':datetimeend'   => $datetimeend
        );

    $sqlmax = "SELECT 
        MAX(temperature) as temperature,
        MAX(humidity) as humidity,
        MAX(pressure) as pressure
        FROM measurement
        WHERE nodeId =:nodeId
        AND datetime BETWEEN :datetimestart 
        AND :datetimeend"; 

    $sqlmin = "SELECT 
        MIN(temperature) as temperature,
        MIN(humidity) as humidity,
        MIN(pressure) as pressure
        FROM measurement
        WHERE nodeId =:nodeId
        AND datetime BETWEEN :datetimestart 
        AND :datetimeend"; 

    $max = $this->queryDatabase($sqlmax, $values);
    $min = $this->queryDatabase($sqlmin, $values);
    $data = array('max' => $max[0],
                  'min' => $min[0]
    );
    return data;

效果很好,但现在我需要选择相应的日期时间'每个最小值和最大值。当然,我可以在数据库中查询每个Max和Min值,但这不是一个很好的解决方案。有两个查询可以做到这一点吗?

修改 两个查询的当前输出都存储在数据数组

$data = Array
(
    [max] => Array
        (
            [temperature] => 29.56
            [humidity] => 74.23
            [pressure] => 1029.02
        ) 
    [min] => Array
        (
            [temperature] => 7.61
            [humidity] => 19.27
            [pressure] => 1019.28
        )

)

但我正在寻找这个:

$data = Array
    (
        [max] => Array
            (
                [temperature] => 29.56
                [t_datetime] => 
                [humidity] => 74.23
                [h_datetime] =>
                [pressure] => 1029.02
                [p_datetime] =>
            ) 
        [min] => Array
            (
                [temperature] => 7.61
                [t_datetime] =>
                [humidity] => 19.27
                [h_datetime] =>
                [pressure] => 1019.28
                [p_datetime] =>
            )
)

2 个答案:

答案 0 :(得分:2)

我建议您使用由union all

连接的六个查询来执行此操作
SELECT *
FROM (SELECT 'max temperature' as which, temperature as value, datetime
      FROM measurement
      WHERE nodeId = :nodeID AND datetime BETWEEN :datetimestart AND :datetimeend
      ORDER BY 2 DESC
      LIMIT 1
     ) t
UNION ALL
SELECT *
FROM (SELECT 'min temperature' as which, temperature as value, datetime
      FROM measurement
      WHERE nodeId =:nodeID AND datetime BETWEEN :datetimestart AND :datetimeend
      ORDER BY 2 ASC
      LIMIT 1
     ) t
UNION ALL
. . .

这有点麻烦,但您可以返回测量表中您希望每个值的所有列。因此,您也可以将湿度控制在最高温度,例如。

答案 1 :(得分:0)

您可以使用结果表和测量

的连接
   $sqlmax = "
   select a.temperature, a.humidity, a.pressure, b.datetime 
   from (   SELECT 
        MAX(temperature) as temperature,
        MAX(humidity) as humidity,
        MAX(pressure) as pressure
        FROM measurement
        WHERE nodeId =:nodeId
        AND datetime BETWEEN :datetimestart 
        AND :datetimeend
  ) a inner join measurement as b on a.temperature = b.temperature 
              and a.humidity = b.humidity and a.pressure = b.pressure " ;