当EndTime
表值为TimeStr
时,我需要显示带有hopper2_setpoint_met
值的1
,并且需要将Weight
显示为当设定点满足表的值为MAX(Value)
时,hopper2_liveweight
的{{1}}表中的TimeStr +3 second
从TimeStr
值开始。
所需结果示例
1
您可以从此SQLFiddle帖子中访问此数据,在该帖子中我构建了架构并插入了数据的一小部分,或者如果您想使用它在自己的MySQL数据库表上创建,那么这里是该SQL的也是
+---------------------+--------+
| EndTime | Weight |
+---------------------+--------+
| 2019-06-30 12:02:33 | 300 |
| 2019-06-30 12:04:45 | 299 |
+---------------------+--------+
我昨天在MySQL to select max value between on and off time stamps plus 3 seconds上发布了关于运输商的类似信息,但我了解到使用temp表并不是最佳解决方案,尽管在那里收到的答案确实给了我所要求的预期结果。 / p>
我还从收到的这些答案中学到了一些东西,并尝试了这些事情的几种变体,例如CREATE TABLE `hopper2_setpoint_met` (
`TimeStr` datetime NOT NULL,
`Value` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `hopper2_setpoint_met` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:02:33',1);
INSERT INTO `hopper2_setpoint_met` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:03:51',0);
INSERT INTO `hopper2_setpoint_met` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:45',1);
INSERT INTO `hopper2_setpoint_met` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:05:56',0);
INSERT INTO `hopper2_setpoint_met` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:49',1);
CREATE TABLE `hopper2_liveweight` (
`TimeStr` datetime NOT NULL,
`Value` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:02:33',292);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:02:34',298);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:02:35',300);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:02:36',300);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:14',106);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:15',111);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:16',116);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:17',123);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:19',132);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:20',142);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:21',148);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:22',154);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:23',160);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:24',166);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:25',172);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:26',177);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:27',184);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:29',190);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:30',195);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:31',201);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:32',208);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:33',213);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:34',220);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:35',226);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:36',232);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:37',239);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:38',244);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:39',251);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:40',257);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:41',263);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:42',269);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:43',276);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:44',282);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:45',288);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:46',293);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:47',299);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:04:48',299);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:18',108);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:19',112);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:20',117);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:21',123);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:22',128);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:23',135);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:24',141);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:25',148);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:26',154);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:27',160);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:28',165);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:29',172);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:30',177);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:31',183);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:32',190);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:33',196);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:34',201);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:35',208);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:36',213);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:37',220);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:38',226);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:39',231);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:40',237);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:41',242);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:42',248);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:43',254);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:44',262);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:45',269);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:46',276);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:47',282);
INSERT INTO `hopper2_liveweight` (`TimeStr`,`Value`) VALUES ('2019-06-30 12:06:49',288);
技巧和其他一些新任务,而我又难以获得预期的结果结果。
如果我将数据放入临时表中,我当然可以得到预期的结果,但是它不能很好地执行,并且我认为它也使过程变得复杂,并且并不是真正需要的。
我使用If(CAST(Value AS UNSIGNED) != 0, Value, Null) Weight
语句和JOIN
数据以及子查询尝试了几种不同的变体,当我尝试获得timestr + 3秒记录之间的最大权重时,我的表现不足。 >
我很想学习新的知识,并且从有助于解决该问题的答案中想到一个灯泡,所以继续前进,这是应用类似逻辑而不需要创建临时或永久临时表的问题。 / p>
将所有内容保留在带有一些UNION
语句的查询中,或者如果需要的话甚至可以包含多个子查询,这对我来说也可以。我不确定使用MySQL会话变量是否会有所帮助,但是我什至尝试了一些尝试并失败了。
答案 0 :(得分:1)
一个合适的查询是:
SELECT sm.TimeStr AS EndTime, Max(lw.Value) AS Weight
FROM (SELECT TimeStr FROM hopper2_setpoint_met WHERE Value = 1) sm
INNER JOIN hopper2_liveweight lw ON (sm.TimeStr + INTERVAL 3 SECOND) = lw.TimeStr
GROUP BY sm.TimeStr
;