我觉得我接近我的解决方案,但并不完全在那里。我想选择testRun.build
两次,
第一次基于当前运行(Results.build_fk
)并基于测试通过的最后一次运行(Results.lastPass
)。
这是我到目前为止的Select语句,我觉得应该可以工作(because of this answer),但它告诉我“Subquery返回多行”。
SELECT Results.build_fk, Results.testCaseID, Results.pass, Results.time,
( SELECT testRun.build
FROM testRun
INNER JOIN Results r1 ON r1.build_fk = testRun.runID
) AS Last_build,
( SELECT testRun.build
FROM testRun
INNER JOIN Results r2 ON r2.lastPass = testRun.runID
)
AS Current_Build
FROM Results
INNER JOIN testCases AS t1 ON t1.testCaseID = Results.testCaseID
ORDER BY build_fk DESC, testCaseID ASC;
以下是样本表,包含样本数据:
Results
(`build_fk`, `testCaseID`, `pass`, `time`, `lastPass`)
(1132, 200, 0, {some timestamp}, 1132)
(1133, 200, 0, {some timestamp}, 1132)
(1134, 200, 1, {some timestamp}, 1134)
(1132, 210, 0, {some timestamp}, 1132)
(1133, 210, 0, {some timestamp}, 1132)
(1134, 210, 1, {some timestamp}, 1134)
testRun
(`runID`, `build`)
(1132, 'build-1.0')
(1133, 'build-1.1')
(1134, 'build-1.2')
抱歉没有SQLFiddle,我无法正确创建表。
答案 0 :(得分:4)
我认为子查询中的其他连接是不必要的:
SELECT r.build_fk, r.testCaseID, r.pass, r.time,
(SELECT tr.build
FROM testRun tr
WHERE r.build_fk = tr.runID
) AS Last_build,
(SELECT tr.build
FROM testRun tr
WHERE r.lastPass = tr.runID
) AS Current_Build
FROM Results r INNER JOIN
testCases t1
ON t1.testCaseID = r.testCaseID
ORDER BY build_fk DESC, testCaseID ASC;
您希望子查询引用外部查询(在此版本中使用r
表别名)。这称为相关子查询。