[英]How to select a column from a table and another column from another table
試圖建立一個數據庫,其中有一個問題表和一個問題答案表。
如何選擇一個問題(來自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");
您的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.