花了一些相当长的时间(感谢Abecee)创建子查询来对表进行排序后,我惊愕地发现虽然这在MySQL Workbench和其他工具中运行得非常快速,但在运行mysql查询时,子查询存在已知问题Java(在JSP中)。我无法更改我的jdbc连接字符串,因此我将不胜感激任何有关是否可以修改以下代码以仅使用连接或其他内容的信息。注意我必须在第一个SELECT中封装3个变量,否则java中的mysql也会引发语法错误。
我是一个SQL新手,所以我正在努力解决这个问题。
SELECT @locationID:=0 as LocVar, @ts:=NULL as tsVar, @changed:=0 as changed,
L4.assetid, L4.node, A.name as Name, L.zonename as Zone, SEC_TO_TIME(duration) as Duration
FROM
(SELECT MIN(assetID) AS assetid, MIN(nodeid) AS node, SUM(secDiff) AS duration, changed
FROM
(SELECT
assetID
, nodeid
, @changed := IF(nodeid <> previousLocationID, @changed + 1, @changed) AS changed
, IFNULL(TIMESTAMPDIFF(SECOND,
previousTs,
ts
),
0
) AS secDiff
FROM
(SELECT
assetID
, nodeid
, @locationID AS previousLocationID
, @locationID := nodeid AS currentLocationID
, ts
, @ts AS previousTs
, @ts := ts AS currentTs
FROM Logs L1
ORDER BY ts
) L2
ORDER BY ts
) L3
GROUP BY changed
) L4
JOIN asset A
ON L4.assetid = A.id
JOIN node L
ON L4.node = L.id
where duration != 0
ORDER BY changed DESC
感谢。 活性