Mysql 时间差和
我跟不上时代的总和差异 在数据库中,它们是重复记录。例如:Mysql 时间差和,mysql,sql,Mysql,Sql,我跟不上时代的总和差异 在数据库中,它们是重复记录。例如: _Status________Information__________Time______ Start(1) | heating on 20°C | 10:00 Start(1) | heating on 21°C | 10:20 Stop(0) | Heating | 11:00 Stop(0) | Wait for instructions |
_Status________Information__________Time______
Start(1) | heating on 20°C | 10:00
Start(1) | heating on 21°C | 10:20
Stop(0) | Heating | 11:00
Stop(0) | Wait for instructions | 12:00
记录A(开始)和C(停止)后的差值为1小时
当我不使用SUM时,我会得到具有正确时差的行
查询
SET @end_time=0;
SELECT (TIME_TO_SEC(t2.date) - TIME_TO_SEC(t1.date)) AS difference, t1.date AS start, t2.date AS stop, @end_time:=t2.date
FROM `heating_history` AS t1
LEFT JOIN `heating_history` AS t2 ON t2.date > t1.date and t2.status!=1
WHERE t1.status=1 and TIME_TO_SEC(t1.date) > TIME_TO_SEC(@end_time)
SET @end_time=0;
SELECT SUM(TIME_TO_SEC(t2.date) - TIME_TO_SEC(t1.date)) AS difference, t1.date AS start, t2.date AS stop, @end_time:=t2.date
FROM `heating_history` AS t1
LEFT JOIN `heating_history` AS t2 ON t2.date > t1.date and t2.status!=1
WHERE t1.status=1 and TIME_TO_SEC(t1.date) > TIME_TO_SEC(@end_time)
结果
difference start stop @end_time:=t2.date
12703 2014-01-29 07:18:32 2014-01-29 10:50:15 2014-01-29 10:50:15
4079 2014-01-29 13:27:12 2014-01-29 14:35:11 2014-01-29 14:35:11
9839 2014-01-29 16:46:12 2014-01-29 19:30:11 2014-01-29 19:30:11
4810 2014-01-29 21:18:11 2014-01-29 22:38:21 2014-01-29 22:38:21
difference start stop @end_time:=t2.date
258536 2014-01-29 07:18:32 2014-01-29 10:50:15 2014-01-29 10:50:15
但当我想要求和的时候,我得到了一个更好的结果 查询
SET @end_time=0;
SELECT (TIME_TO_SEC(t2.date) - TIME_TO_SEC(t1.date)) AS difference, t1.date AS start, t2.date AS stop, @end_time:=t2.date
FROM `heating_history` AS t1
LEFT JOIN `heating_history` AS t2 ON t2.date > t1.date and t2.status!=1
WHERE t1.status=1 and TIME_TO_SEC(t1.date) > TIME_TO_SEC(@end_time)
SET @end_time=0;
SELECT SUM(TIME_TO_SEC(t2.date) - TIME_TO_SEC(t1.date)) AS difference, t1.date AS start, t2.date AS stop, @end_time:=t2.date
FROM `heating_history` AS t1
LEFT JOIN `heating_history` AS t2 ON t2.date > t1.date and t2.status!=1
WHERE t1.status=1 and TIME_TO_SEC(t1.date) > TIME_TO_SEC(@end_time)
结果
difference start stop @end_time:=t2.date
12703 2014-01-29 07:18:32 2014-01-29 10:50:15 2014-01-29 10:50:15
4079 2014-01-29 13:27:12 2014-01-29 14:35:11 2014-01-29 14:35:11
9839 2014-01-29 16:46:12 2014-01-29 19:30:11 2014-01-29 19:30:11
4810 2014-01-29 21:18:11 2014-01-29 22:38:21 2014-01-29 22:38:21
difference start stop @end_time:=t2.date
258536 2014-01-29 07:18:32 2014-01-29 10:50:15 2014-01-29 10:50:15
谢谢您的帮助。您的问题来自于没有指定要求和的内容,因为没有group by子句,它只是求和您所有的内容。要确保只求和所需的差异,请以这种方式使用子选择查询
SELECT SUM(DIFFERENCE),start,stop
FROM
(
SELECT (TIME_TO_SEC(t2.date) - TIME_TO_SEC(t1.date)) AS difference, t1.date AS start, t2.date AS stop, @end_time:=t2.date
FROM `heating_history` AS t1
LEFT JOIN `heating_history` AS t2 ON t2.date > t1.date and t2.status!=1
WHERE t1.status=1 and TIME_TO_SEC(t1.date) > TIME_TO_SEC(@end_time) and DATE(t1.date)=DATE_SUB(CURDATE(), INTERVAL 1 day)) as MyQuery
您不能在查询中抛出
SUM
,期望得到正确的结果。您需要指定GROUP BY
子句或使用Sub Select对正确的结果进行求和。这是SQL Server还是MySQL或其他什么?还可以添加返回正确行的查询和返回错误SUMI use MySQL的查询。添加的查询