Этот код покажет вам список вопросов и ответы на них правильно или нет.
select
A.Question,
min(1) as QuestionsCount,
-- if this evaluates to null, they got A) the answer wrong or B) this portion of the answer wrong
-- we use MIN() here because we want to mark multi-answer questions as wrong if any part of the answer is wrong.
min(case when Q.idEvaluation IS NULL then 0 else 1 end) as QuestionsCorrect
from
ExamAnswers as A
left join ExamQuestions as Q on Q.Question = A.Question and Q.CorrectAnswer = A.ChosenAnswer
group by
A.Question -- We group by question to merge multi-answer-questions into 1
Выход подтвержден:
Обратите внимание, что столбцы намеренно названы таким образом, так как они должны быть включены в качестве подзапроса в части 2 ниже.
Этот код даст вам результат теста.
select
sum(I.QuestionsCorrect) as AnswersCorrect,
sum(I.QuestionsCount) as QuestionTotal,
convert(float,sum(I.QuestionsCorrect)) / sum(I.QuestionsCount) as PercentCorrect -- Note, not sure of the cast-to-float syntax for MySQL
from
(select
A.Eval,
A.Question,
min(1) as QuestionsCount,
min(case when Q.idEvaluation IS NULL then 0 else 1 end) as QuestionsCorrect
from
ExamAnswers as A
left join ExamQuestions as Q on Q.Question = A.Question and Q.CorrectAnswer = A.ChosenAnswer
where
A.Eval = 25
group by
A.Question, A.Eval) as I
group by
I.Eval
Выход подтвержден:
Это сообщит общую концепцию. Мне трудно понять названия ваших столбцов idEvaluation
и Eval
, но я уверен, что вы можете изменить приведенный выше код в соответствии с вашими целями.
Обратите внимание, я сделал это на сервере sql, но я использовал довольно базовую функциональность SQL, поэтому он должен хорошо переводиться на MySQL.