【问题标题】:time avg within a mysql database within a certain time period某个时间段内mysql数据库中的时间平均值
【发布时间】:2018-01-11 10:03:12
【问题描述】:

所以我试图在 20 分钟内获取时间平均值并将其从我的主 sql 表添加到辅助表中,该数字将在一天中多次进入数据库,并且在第一次进入该数字时运行良好但是如果它在当天晚些时候进入,它会将辅助表更新为更高的平均值,例如

58 号在上午 12:08 进入,然后在上午 12:31 停止进入,如果它在下午 13:47 回到表中并在下午 14:13 退出,则平均值将显示 23 分钟小时 5 分钟

我正在使用

insert into test.time (beacon,location,date,time_avg,mac,time)
SELECT beacon,location,date, 
TIMEDIFF(max(`time`),min(`time`)) AS `time_avg`,mac,time
FROM test.test where time > now() - interval 30 minute
group by beacon
ORDER BY `time_avg` DESC

我如何查询它以便它在半小时左右添加一个新行,所以我确实尝试使用一个事件,它每 30 分钟而不是每次更新后才查询一次,但我仍然遇到同样的问题,我也尝试过在 mac 是唯一值的地方插入忽略并替换为。

我附上了一张我的表格的图片,以便了解其中的数据。

字符串中的示例数据方便

insert into test ('location','beacon','mac','date','time') VALUES (YELLOW, 1,AC3422845D, 2018-01-10, 12:08:55);

mysql table data

【问题讨论】:

  • 我们不能使用图片,您可以将您的示例数据作为文本添加到问题中。
  • 如果我对您的理解正确,每个信标/位置/mac/date 都有未知次数,您希望计算属于 20 分钟间隔的时间块的时间差异,并通过一天中的块数(到目前为止)。你什么时候开始和结束这 20 分钟的时间段(00:00 - 24:00?)
  • @P.Salmon 是的,所以它是一个持续运行的系统到实际设定的时间并不重要,但是我试图实现的是它只组合在一起并计算出数字为 1 的时间之间的差异看到以及在 20-30 分钟间隔内最后一次看到该数字的时间,但它似乎没有这样做,只要系统运行并且第一次在当天和最后一次看到该数字,它就会计算它
  • 您的查询中有许多看起来错误的事情 1 - 尝试将 6 个字段插入到预期有 5 个字段的表中(位置?) 2 - 您正在比较时间与日期时间(now() )) 如果在最后 30 分钟内,这将导致包含任何先前日期的任何时间,group by 可能导致不确定的位置和日期,并且如果设置了 only_full_group_by 将失败(并且默认情况下在较新版本的 mysql 中)。

标签: mysql


【解决方案1】:

您有很多事情要查看,并且可能会在您的 insert..select 中修复。如果您的目标是在每个信标、mac、日期的插入表中有 1 条记录,那么在重复键上插入可能就是您想要的。例如

MariaDB [沙盒]> 如果存在则删除表 t; 查询正常,0 行受影响(0.27 秒)

MariaDB [sandbox]>
MariaDB [sandbox]> create table t
    -> (location varchar(20),beacon int, mac varchar(20),`date` date, `time` time);
Query OK, 0 rows affected (0.20 sec)

MariaDB [sandbox]> insert into t (location,beacon,mac,`date`,`time`) VALUES ('YELLOW', 1,'AC3422845D', '2018-01-10', '11:00:44');
Query OK, 1 row affected (0.01 sec)

MariaDB [sandbox]> insert into t (location,beacon,mac,`date`,`time`) VALUES ('YELLOW', 1,'AC3422845D', '2018-01-11', '10:00:55');
Query OK, 1 row affected (0.01 sec)

MariaDB [sandbox]> insert into t (location,beacon,mac,`date`,`time`) VALUES ('YELLOW', 1,'AC3422845D', '2018-01-11', '11:00:55');
Query OK, 1 row affected (0.05 sec)

MariaDB [sandbox]> insert into t (location,beacon,mac,`date`,`time`) VALUES ('YELLOW', 1,'AC3422845D', '2018-01-11', '12:00:55');
Query OK, 1 row affected (0.02 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> drop table if exists t1;
Query OK, 0 rows affected (0.08 sec)

MariaDB [sandbox]> create table t1
    -> (beacon int,location varchar(20), mac varchar(20),`date` date, maxtimetime  time, mintime time, obs int, now30 datetime);
Query OK, 0 rows affected (0.26 sec)

MariaDB [sandbox]> alter table t1
    -> add unique key  t1k1 (beacon,mac,`date`);
Query OK, 0 rows affected (0.18 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]>
MariaDB [sandbox]> #select * from t;
MariaDB [sandbox]> #select @now;
MariaDB [sandbox]>
MariaDB [sandbox]> truncate table t1;
Query OK, 0 rows affected (0.27 sec)

MariaDB [sandbox]> set @now = str_to_date('2018-01-10 11:15:00','%Y-%m-%d %H:%i:%s');
Query OK, 0 rows affected (0.00 sec)

MariaDB [sandbox]> insert into t1 (beacon ,mac ,`date`, maxtimetime, mintime, obs, now30)
    -> select *
    -> from
    -> (select t.beacon,t.mac,t.`date`, max(`time`) maxtime,min(`time`) mintime,count(*) obs,
    -> @now - interval 30 minute now30
    -> from t
    -> where `time`  between time(@now - interval 30 minute) and time(@now) and `date` = date(@now)
    -> group by t.beacon ,t.mac,t.`date`
    -> ) s
    -> on duplicate key
    -> update maxtimetime = s.maxtime, mintime = s.mintime, obs = s.obs, now30 = s.now30;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> select * from t1;
+--------+----------+------------+------------+-------------+----------+------+---------------------+
| beacon | location | mac        | date       | maxtimetime | mintime  | obs  | now30               |
+--------+----------+------------+------------+-------------+----------+------+---------------------+
|      1 | NULL     | AC3422845D | 2018-01-10 | 11:00:44    | 11:00:44 |    1 | 2018-01-10 10:45:00 |
+--------+----------+------------+------------+-------------+----------+------+---------------------+
1 row in set (0.00 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> set @now = str_to_date('2018-01-11 11:15:00','%Y-%m-%d %H:%i:%s');
Query OK, 0 rows affected (0.00 sec)

MariaDB [sandbox]> insert into t1 (beacon ,mac ,`date`, maxtimetime, mintime, obs, now30)
    -> select *
    -> from
    -> (select t.beacon,t.mac,t.`date`, max(`time`) maxtime,min(`time`) mintime,count(*) obs,
    -> @now - interval 30 minute now30
    -> from t
    -> where `time` between time(@now - interval 30 minute) and time(@now) and `date` = date(@now)
    -> group by t.beacon ,t.mac,t.`date`
    -> ) s
    -> on duplicate key
    -> update maxtimetime = s.maxtime, mintime = s.mintime, obs = s.obs, now30 = s.now30;
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> select * from t1;
+--------+----------+------------+------------+-------------+----------+------+---------------------+
| beacon | location | mac        | date       | maxtimetime | mintime  | obs  | now30               |
+--------+----------+------------+------------+-------------+----------+------+---------------------+
|      1 | NULL     | AC3422845D | 2018-01-10 | 11:00:44    | 11:00:44 |    1 | 2018-01-10 10:45:00 |
|      1 | NULL     | AC3422845D | 2018-01-11 | 11:00:55    | 11:00:55 |    1 | 2018-01-11 10:45:00 |
+--------+----------+------------+------------+-------------+----------+------+---------------------+
2 rows in set (0.00 sec)

MariaDB [sandbox]>
MariaDB [sandbox]> set @now = str_to_date('2018-01-11 12:15:00','%Y-%m-%d %H:%i:%s');
Query OK, 0 rows affected (0.00 sec)

MariaDB [sandbox]> insert into t1 (beacon ,mac ,`date`, maxtimetime, mintime, obs, now30)
    -> select *
    -> from
    -> (select t.beacon,t.mac,t.`date`, max(`time`) maxtime,min(`time`) mintime,count(*) obs,
    -> @now - interval 30 minute now30
    -> from t
    -> where `time`  between time(@now - interval 30 minute) and time(@now) and `date` = date(@now)
    -> group by t.beacon ,t.mac,t.`date`
    -> ) s
    -> on duplicate key
    -> update maxtimetime = s.maxtime, mintime = s.mintime, obs = s.obs, now30 = s.now30;
Query OK, 2 rows affected (0.02 sec)
Records: 1  Duplicates: 1  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> select * from t1;
+--------+----------+------------+------------+-------------+----------+------+---------------------+
| beacon | location | mac        | date       | maxtimetime | mintime  | obs  | now30               |
+--------+----------+------------+------------+-------------+----------+------+---------------------+
|      1 | NULL     | AC3422845D | 2018-01-10 | 11:00:44    | 11:00:44 |    1 | 2018-01-10 10:45:00 |
|      1 | NULL     | AC3422845D | 2018-01-11 | 12:00:55    | 12:00:55 |    1 | 2018-01-11 11:45:00 |
+--------+----------+------------+------------+-------------+----------+------+---------------------+
2 rows in set (0.00 sec)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2018-05-29
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    相关资源
    最近更新 更多