php mysql匹配并返回值

时间:2013-04-16 08:53:08

标签: mysql sql

我的表是:

studens_exam_answers_tbl
----------------------------------------------------
exam_id | question_id | student_id  | student_answer 
----------------------------------------------------
    10       1           6              1 
    10       2           6              4
    10       3           6              3
    10       4           6              0
    10       5           6              5
    10       1           5              1
    10       2           5              2
    10       3           5              2
    10       4           5              5
    10       5           5              5

question_poll__tbl
--------------------------------------------------------------------------------
question_id | question  | option_1 |  option_2 | ....| option_5 |correct_answer
--------------------------------------------------------------------------------
    1          ....         ....       ....      ...    ....        5
    2          ....         ....       ....      ...    ....        2
    3          ....         ....       ....      ...    ....        3
    4          ....         ....       ....      ...    ....        4
    5          ....         ....       ....      ...    ....        1

我希望将学生的答案与sql的正确答案相匹配。如果学生回答是正确的'这是真的'否则'假'。如果student_answer为0'空'。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

试试这个

 Select student id, (Case when student_answer=0
    then ""
    else
    when student_answer=correct_answer then "true"
    else "false"
    end ) as Result
    from studens_exam_answers_tbl inner join question_poll__tbl on studens_exam_answers_tbl.question_id.question_id =question_poll__tbl.question_id