我正在努力制作单个mysql查询以从表中获取数据,该表包含有关包含测试用例的测试请求结果的信息。每个测试用例可以有4种不同的结果。结构如下所示(带有示例数据):
testrequestsresultsID | testrequestsID | testcaseresultsID
1 | 1 | 4
2 | 1 | 2
3 | 1 | 1
4 | 1 | 1
5 | 2 | 4
6 | 2 | 4
7 | 2 | 2
8 | 2 | 2
我的基本目标是在4个不同的列中显示有关测试请求进度的信息。
我设法通过以下查询获得一个测试请求ID的信息:
SELECT
(SELECT COUNT(*) AS `count` FROM `testrequestsresults` WHERE `testrequestsID`=1) `total`,
(SELECT COUNT(*) AS `count` FROM `testrequestsresults` WHERE `testrequestsID`=1 AND `testcaseresultsID`<>4) `finished`,
(SELECT COUNT(*) AS `count` FROM `testrequestsresults` WHERE `testrequestsID`=1 AND `testcaseresultsID`=1) `pass`,
(SELECT COUNT(*) AS `count` FROM `testrequestsresults` WHERE `testrequestsID`=1 AND `testcaseresultsID`=2) `fail`
仅显示一个请求的信息。
我的问题是如何显示许多行的扩展信息以及显示testrequestsID的前栏?考虑到示例数据,它应该如下所示:
testrequestsID | total | finished | pass | fail
1 | 4 | 3 | 2 | 1
2 | 4 | 2 | 0 | 2
提前感谢您的帮助。
答案 0 :(得分:1)
SELECT testrequestsID,
SUM IF(`testrequestsID`=1, 1,0) as total,
SUM IF(`testrequestsID`=1 AND `testcaseresultsID`<>4, 1, 0) AS finished,
SUM IF(`testrequestsID`=1 AND `testcaseresultsID`=1, 1, 0) AS pass,
SUM IF(`testrequestsID`=1 AND `testcaseresultsID`=2, 1, 0) AS fail
FROM
GROUP BY testrequestsID
答案 1 :(得分:0)
SELECT testrequestsid
, COUNT(*) total
, SUM(testcaseresultsid <> 4) finished
, SUM(testcaseresultsid = 1) pass
, SUM(testcaseresultsid = 2) fail
FROM my_table
GROUP
BY testrequestsid;