У меня есть таблица userSection
(вопросы таблицы), которая имеет userSectionId
и я могу иметь соответствующий ответ в любом из таблицы ответов isee_answers_2013
или isee_answers_2014
или isee_answers_2015
или isee_answers_2016
. Текущий запрос есть,Определения соответствующих записей из таблиц
select us.userSectionId,qs.questionSectionId,qs.questionId,us.userId,
case
when a13.correct is not null then a13.answerId
when a14.correct is not null then a14.answerId
when a15.correct is not null then a15.answerId
when a16.correct is not null then a16.answerId
end
as AnswerId,
case
when a13.correct is not null then a13.correct
when a14.correct is not null then a14.correct
when a15.correct is not null then a15.correct
when a16.correct is not null then a16.correct
end
as Correct,
case
when a13.correct is not null then a13.duration
when a14.correct is not null then a14.duration
when a15.correct is not null then a15.duration
when a16.correct is not null then a16.duration
end
as Duration
from userSections us
join questionSections qs on qs.sectionId = us.sectionId
JOIN
userExams ue
ON
ue.userExamId = us.userExamId
left join
isee_answers_2013.answers a13 on us.userSectionId=a13.userSectionId and us.UserId = a13.userId
left join
isee_answers_2014.answers a14 on us.userSectionId=a14.userSectionId and us.UserId = a14.userId
left join
isee_answers_2015.answers a15 on us.userSectionId=a15.userSectionId and us.UserId = a15.userId
left join
isee_answers_2016.answers a16 on us.userSectionId=a16.userSectionId and us.UserId = a16.userId
WHERE
us.valid=1 and ue.userExamId=20467
Может кто-нибудь пожалуйста проверить и сказать, если это правильный подход, или есть какой-либо другой лучший способ сделать это?
Спасибо, Гордон! –