有三個表,其中single表存放了單選題的資訊,examquestions表存放了某個試卷各個部分的試題組成,這里僅關注單選題型,
answer 表存放的是考生的答題資訊,即考生如果做了試卷的某些題目,則把這些題目及答題內容插入到answer表。各個表主要的欄位如下。
kaoshi_single表: singleid, ssubject, Sanswer
kaoshi_examquestions表: examquestionid,questionid,questiontype,examid
kaoshi_answer表:answerid,xuehao,examid,answer,questionid,questiontype
現在我想查詢某個學生的考試答案以及原始題目及正確答案,sql為select * from kaoshi_single s join kaoshi_examquestions e join kaoshi_answer a where a.questionid = e.questionid and a.questiontype='single' and s.singleid=e.questionid and e.questiontype='single' and e.examid='"+examid+"' and a.xuehao='"+xuehao+"'";
其中最后一行的加號前后的examid和xuehao為變數。那么這個陳述句有個問題,即學生如果做試題時某題目沒有做(留空),那么這個題目的答題資訊就沒有插入answer表,因此通過上述sql陳述句查看答案時該題目并不會顯示出來。現在我想把某個試卷的單選題的資訊及學生的答題資訊都查出來,即使學生沒做,也要列出題目,只是學生答案顯示為空。 按照我的分析應該是要用左外連接,但我用下面左外連接也沒用。
sql="select * from kaoshi_single s left join kaoshi_examquestions e on s.singleid=e.questionid left join kaoshi_answer a on a.questionid = e.questionid where a.questiontype='single' and e.questiontype='single' and e.examid='"+examid+"' and a.xuehao='"+xuehao+"'";
請教一下高手要怎樣寫,最好還能簡要的解釋一下。謝謝。
uj5u.com熱心網友回復:
有沒有高手有好的想法呀?uj5u.com熱心網友回復:
@二月十六 @shoppo0505轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/147624.html
標籤:MySQL
上一篇:概率論與數理統計
