Mysql 从表中识别匹配的记录
我有一个表Mysql 从表中识别匹配的记录,mysql,sql,Mysql,Sql,我有一个表userSection(问题表),它有一个userSectionId,我可以在任何答案表isee\u answers\u 2013或isee\u answers\u 2014或isee\u answers\u 2015或isee\u answers\u 2016中找到相应的答案。当前查询已被删除 select us.userSectionId,qs.questionSectionId,qs.questionId,us.userId, case when a13.correct is
userSection
(问题表),它有一个userSectionId
,我可以在任何答案表isee\u answers\u 2013
或isee\u answers\u 2014
或isee\u answers\u 2015
或isee\u answers\u 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
是否有人可以验证并说这是正确的方法,或者是否有其他更好的方法可以做到这一点?假设此逻辑正确,您可能只需要使用
coalesce()
:
等等
逻辑并不完全相同。如果匹配答案上的
answerId
可以是NULL
,那么这并不能满足您的需要。否则,这是表示此逻辑的一种更简单的方法。假设此逻辑正确,您可能只需使用coalesce()
:
等等
逻辑并不完全相同。如果匹配答案上的answerId
可以是NULL
,那么这并不能满足您的需要。否则,这是一种更简单的表达这种逻辑的方法
coalesce(a13.answerId, a14.answerId, a15.answerId, a16.answerId) as answerId