2013-04-16 102 views
1

我的表是:PHP的MySQL的匹配和返回值

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'空'。

我该怎么办?

+0

你想这样做SQL手段?您想要在studens_exam_answers_tbl中添加另一列以存储true或false。 – Lakshmi

+0

你有没有试过任何查询? –

+0

加入和比较,它并不难...... – Shomz

回答

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 
+0

您使用的是同一个表中的列发布应该是studens_exam_answers_tbl.question_id = question_poll__tbl.question_id前检查 – Lakshmi

+0

对不起,我复制的名字,所以我错过了我让它正确的,现在 –

+0

有一个在'corrct_answer'一个typ0。应该是'correct_answer'。 – BlitZ