【问题标题】:mysql selecting and making an averagemysql 取平均值
【发布时间】:2015-02-19 19:17:18
【问题描述】:

我有一个存储温度、传感器编号和输入时间的数据库。了解我对此很陌生,但非常感谢您的帮助!

数据库字段:

temp,tempdate,tempttime,sensorno, timeentered

我要做的是编写一个在 php 中运行的查询,该查询将选择当天的数据,查看时间并平均每小时的温度。因此,它将采用属于同一小时的所有数据,即从 00:00:00 到 00:01:00(每个传感器)将其相加,然后除以存储的值的数量,得出该小时的平均值。

示例数据:

temp tempdate   tempttime sensorno  timeentered
5   2014-12-21  00:00:00    1   2014-12-21 00:00:00
8.5 2014-12-21  00:00:01    2   2014-12-21 00:00:01
9   2014-12-21  00:00:02    3   2014-12-21 00:00:02
4.5 2014-12-21  00:00:02    4   2014-12-21 00:00:02
9   2014-12-21  00:00:03    5   2014-12-21 00:00:03
5.5 2014-12-21  00:00:03    6   2014-12-21 00:00:03
5   2014-12-21  00:01:08    1   2014-12-21 00:01:08
8.5 2014-12-21  00:01:09    2   2014-12-21 00:01:09
9   2014-12-21  00:01:09    3   2014-12-21 00:01:09
5   2014-12-21  00:01:10    4   2014-12-21 00:01:10
9   2014-12-21  00:01:10    5   2014-12-21 00:01:10
5.5 2014-12-21  00:01:11    6   2014-12-21 00:01:11
5   2014-12-21  00:02:16    1   2014-12-21 00:02:16
8.5 2014-12-21  00:02:16    2   2014-12-21 00:02:16
9   2014-12-21  00:02:17    3   2014-12-21 00:02:17
5   2014-12-21  00:02:18    4   2014-12-21 00:02:18
9   2014-12-21  00:02:18    5   2014-12-21 00:02:18
5.5 2014-12-21  00:02:19    6   2014-12-21 00:02:19
5   2014-12-21  00:03:24    1   2014-12-21 00:03:24
8.5 2014-12-21  00:03:24    2   2014-12-21 00:03:24
8.5 2014-12-21  00:03:25    3   2014-12-21 00:03:25
4.5 2014-12-21  00:03:25    4   2014-12-21 00:03:25
9   2014-12-21  00:03:26    5   2014-12-21 00:03:26
5.5 2014-12-21  00:03:26    6   2014-12-21 00:03:26

所以我当前的查询是

use ponddb;
SELECT 
    *
FROM
    pondtemp
WHERE
    timeentered >= CURDATE();

【问题讨论】:

    标签: mysql select filtering


    【解决方案1】:

    这基本上就是您想要的查询:

    SELECT tempdate, hour(temptime) as temphour, sensorno, avg(temp) as avgtemp
    FROM pondtemp
    WHERE tempdate = CURDATE()
    GROUP BY tempdate, hour(temptime), sensorno
    ORDER BY tempdate, hour(temptime), sensorno;
    

    我包含了日期和时间,以防您想扩展 where 子句以处理多个日期。

    【讨论】:

    • 完美,我有最后一个问题 - 因为我是新手,我使用 phplot 来显示图表,但我不确定我在代码中使用了正确的名称 - 虽然它在 mysqlworkbench 中完美运行: if ($row = mysql_fetch_object($result)) { $sensorno = $row->sensorno; $timeentered = $row->hour; $temp = $row-> 温度;开关($sensorno)
    • @JezWalton 。 . .我在结果集中为合理的名称添加了列别名。
    • Gordon 非常感谢 - 不太清楚如何在图形代码中使用列别名 - 将尝试谷歌搜索它吗?
    • 结果集有四列,tempdatetemphoursensornoavgtemp。您可以像访问结果集中的任何列一样访问它们。
    • 甚至像这样 { $sensorno = $row->sensorno; $timeentered = $row->temphour; $temp = $row-> avgtemp;注意:未定义的属性:C:\xampp\htdocs\jez\AVEPondTempGraphtodayonly.php 中的第 56 行中的未定义属性:stdClass::$hour 注意:未定义的属性:C:\xampp 中的 stdClass::$avgtemp \htdocs\jez\AVEPondTempGraphtodayonly.php 第 57 行
    【解决方案2】:

    如果您的小时字段是日期时间类型,以下应该可以工作

    select avg(temp) temp_avg, hour(tempttime) time_hour from my_table
    where tempdate=curdate()
    group by time_hour
    

    如果它的类型是字符串,那么使用 left(hour,2) 代替 hour(tempttime)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 2016-06-10
      • 2022-01-08
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      相关资源
      最近更新 更多