试图建立一个数据库,该数据库具有一个问题表和一个问题答案表。
如何选择一个问题(来自poll_question)和答案(来自poll_answer)?
CREATE TABLE poll_question(Id_question int primary key not null, question varchar(60));
CREATE TABLE poll_answer(Id_answer int primary key not null, answer varchar(100));
INSERT INTO poll_question(Id_questao, questao)
VALUES(1,"What kind of paper is allowed in tommorows exam?");
INSERT INTO poll_answer(Id_answer,answer)
VALUES(1,"A4 squared sheet");
INSERT INTO poll_answer(Id_answer,answer)
VALUES(2,"A4 lined sheet");
答案 0 :(得分:3)
您的poll_answer
表不完整。它需要另一列来表明每个答案属于哪个问题,例如
CREATE TABLE poll_answer(Id_answer int primary key not null, Id_question int, answer varchar(100));
INSERT INTO poll_answer(Id_answer,Id_question,answer)
VALUES(1,1,"A4 squared sheet"),
(2,1,"A4 lined sheet");
然后,您可以使用JOIN
找到给定问题的答案:
SELECT q.question, a.answer
FROM poll_question q
JOIN poll_answer a ON a.Id_question = q.Id_question
输出:
question answer
What kind of paper is allowed in tommorows exam? A4 squared sheet
What kind of paper is allowed in tommorows exam? A4 lined sheet