查询从两个表中获取数据

时间:2017-03-14 11:27:11

标签: sql

我有两张表QUESTIONSSTUDENT_ANSWER。每个表都有PAPER_ID列和QUESTION_NOSTUDENT_ANSWER有一列STUDENT_ID,如果学生提交论文,那么在STUDENT_ANSWER表中存储{{1} },STUDENT_IDQUESTION_NOSTUDENT_ANSWER

我希望来自PAPER_ID表格的Answer QUESTIONS来自PAPER_ID 'Basic Computer'的{​​{1}}和Answers来自表STUDENT_ANSWER的存储学生答案其中PAPER_ID'Basic Computer' STUDENT_ID1

select 
    q.answer, s.student_ans 
from 
    questions q, student_answer s 
where 
    s.student_id = 1 
    and q.paper_id = 'Basic Computer'
    and s.paper_id = 'Basic Computer';

1 个答案:

答案 0 :(得分:0)

select s.student_id,
       q.paper_id,
       q.question_no,
       q.answer, 
       s.student_answer
  from questions      q, 
       student_answer s 
 where s.student_id  = 1 
   and q.paper_id    = 'Basic Computer'
   and s.paper_id    = q.paper_id
   and s.QUESTION_NO = q.QUESTION_NO

-- result
student_id | paper_id       | question_no | answer | student_answer
-------------------------------------------------------------------
1          | Basic Computer | 1           | red    | blue
1          | Basic Computer | 2           | green  | yellow
1          | Baisc Computer | 3           | blue   | brown

这就是你要找的东西吗?