我在下面查询是否获得考试状态是否失败,暂停和通过。与页面上的其他查询一起,以便我的页面有足够的时间来加载。我已经优化了其他查询。
如何组合这些查询以快速获得结果?可能所有这些查询都可以使用“CASE WHEN”进行组合,任何人都可以帮我优化以下查询吗?
SELECT COUNT(*) AS countFailMT FROM questionPaper WHERE subjectTopic like 'MT%' result=0)
SELECT COUNT(*) AS countPauseMT FROM questionPaper WHERE subjectTopic like 'MT%' result=-1)
SELECT COUNT(*) AS countPassMT FROM questionPaper WHERE subjectTopic like 'MT%' result>=1)
SELECT COUNT(*) AS countFailEN FROM questionPaper WHERE subjectTopic like 'EN%' result=0)
SELECT COUNT(*) AS countPauseEN FROM questionPaper WHERE subjectTopic like 'EN%' result=-1)
SELECT COUNT(*) AS countPassEN FROM questionPaper WHERE subjectTopic like 'EN%' result>=1)
答案 0 :(得分:2)
您想要使用条件聚合。这在MySQL很容易:
select sum(subjectTopic like 'MT%' and result=0) as countFailMT,
sum(subjectTopic like 'MT%' and result=-1) as countPauseMT,
sum(subjectTopic like 'MT%' and result>=1) as countPassMT,
sum(subjectTopic like 'EN%' and result=0) as countFailEN,
sum(subjectTopic like 'EN%' and result=-1) as countPauseEN,
sum(subjectTopic like 'EN%' and result>=1) as countPassEN
from questionPaper;
我假设where
子句中的条件由AND
连接,在创建问题时它以某种方式丢失了。