我有一个具有以下列名称的表:audit_name、audit_choice 和“slno”作为自动增量主键,并且对输出数据没有用处。
audit_name 可以重复,并且 audit_choice 值仅限于某些值,例如“Passed”、“Failed”。
我的查询预期输出是,获取每个“Audit_name”的“通过”计数和“失败”计数。
这是我尝试过的查询,但执行大约 1,000 条记录大约需要 3-4 分钟。
SELECT audit_name,
(SELECT COUNT(*) AS Passed FROM audit AS p1
WHERE p1.audit_name=p2.audit_name AND p1.audit_choice="Passed") AS Passed,
(SELECT COUNT(*) AS Failed FROM audit AS p3
WHERE p3.audit_name=p2.audit_name AND p2.audit_choice="Failed") AS Failed
FROM audit AS p2
GROUP BY audit_name
请建议我如何优化查询。