2013-04-16 142 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