在MySQL中为列创建排名变量

时间:2016-03-04 08:43:23

标签: mysql sql

我需要创建一个排名变量,它基本上就是一个计数器。编写查询以获取UserId,ExamId。对于每个用户ID,可能有多个考试,我需要创建一个新列来获取考试索引。例如:在下面提到的表格中

UserId ExamId CounterVariable

2      3      2-1
2      4      2-2
3      5      3-1
3      6      3-2

SQL:

select us.grade as Grade,k.keyword as QuestionCategory,us.userid as UserId,ue.userExamId as ExamId,ue.completed as ExamCompletedDate,us.userSectionId as UserSectionId,qs.questionSectionId as QuestionSectionId,
    coalesce(a13.answerId, a14.answerId, a15.answerId, a16.answerId) as AnswerId,
    coalesce(a13.answer, a14.answer, a15.answer, a16.answer) as Answer,
    coalesce(a13.correct, a14.correct, a15.correct, a16.correct) as Correct,
    coalesce(a13.flagged, a14.flagged, a15.flagged, a16.flagged) as Flagged,
    coalesce(a13.duration, a14.duration, a15.duration, a16.duration) as Duration,
    coalesce(a13.blank, a14.blank, a15.blank, a16.blank) as Blank
  from userSections us
  join questionSections qs on qs.sectionId = us.sectionId
  join questions q on q.questionId = qs.questionId
  join keywords k on q.keywordId = k.keywordId
  JOIN userExams ue ON ue.userExamId = us.userExamId
  left join
    isee_answers_2013.answers a13 on us.userSectionId = a13.userSectionId and qs.questionSectionId = a13.questionSectionId
  left join
    isee_answers_2014.answers a14 on us.userSectionId = a14.userSectionId and qs.questionSectionId = a14.questionSectionId
  left join
    isee_answers_2015.answers a15 on us.userSectionId = a15.userSectionId and qs.questionSectionId = a15.questionSectionId
  left join
    isee_answers_2016.answers a16 on us.userSectionId = a16.userSectionId and qs.questionSectionId = a16.questionSectionId
 WHERE
    us.valid=1 and us.userid=56
    order by us.grade,k.keyword,us.userid,ue.userExamId,us.userSectionId,qs.questionSectionId

1 个答案:

答案 0 :(得分:1)

我猜你需要为你发布的查询添加一个排名列。

在这种情况下,请使用以下内容:

SELECT rank, CONCAT(UserId, '-', rank) as CounterVariable, Grade, QuestionCategory, UserId, ExamId, ExamCompletedDate, UserSectionId,
       QuestionSectionId, AnswerId, Answer, Correct, Flagged, Duration, Blank
  FROM (SELECT @rn:=CASE WHEN @rn <> us.userid THEN 1 ELSE @rn+1 END AS rank,
               @usr:=us.userid as usr,
               us.grade as Grade, k.keyword as QuestionCategory, us.userid as UserId,
               ue.userExamId as ExamId, ue.completed as ExamCompletedDate, us.userSectionId as UserSectionId,
               qs.questionSectionId as QuestionSectionId,
               coalesce(a13.answerId, a14.answerId, a15.answerId, a16.answerId) as AnswerId,
               coalesce(a13.answer, a14.answer, a15.answer, a16.answer) as Answer,
               coalesce(a13.correct, a14.correct, a15.correct, a16.correct) as Correct,
               coalesce(a13.flagged, a14.flagged, a15.flagged, a16.flagged) as Flagged,
               coalesce(a13.duration, a14.duration, a15.duration, a16.duration) as Duration,
               coalesce(a13.blank, a14.blank, a15.blank, a16.blank) as Blank
          FROM userSections us
          join questionSections qs on qs.sectionId = us.sectionId
          join questions q on q.questionId = qs.questionId
          join keywords k on q.keywordId = k.keywordId
          JOIN userExams ue ON ue.userExamId = us.userExamId
          left join
    isee_answers_2013.answers a13 on us.userSectionId = a13.userSectionId and qs.questionSectionId = a13.questionSectionId
           left join
    isee_answers_2014.answers a14 on us.userSectionId = a14.userSectionId and qs.questionSectionId = a14.questionSectionId
          left join
    isee_answers_2015.answers a15 on us.userSectionId = a15.userSectionId and qs.questionSectionId = a15.questionSectionId
          left join
    isee_answers_2016.answers a16 on us.userSectionId = a16.userSectionId and qs.questionSectionId = a16.questionSectionId
          join (SELECT @rn:=0) t1
          join (SELECT @usr=0) t2
         WHERE us.valid=1 and us.userid=56
         ORDER BY us.grade,k.keyword,us.userid,ue.userExamId,us.userSectionId,qs.questionSectionId
        ) AS tab;