1

我有以下表格

  1. 用户
  2. 问题
  3. 问题类别
  4. 答案

现在我很困惑我的用户类是否应该有任何问题链接或只是答案

用户将回答表格上显示的问题。

所以每年对于不同的问题都会有不同的答案

4

3 回答 3

2

我假设一个用户可以提出问题,一个问题可以属于一个类别,并且许多用户可以发布答案。实际上,它是关于像 stackoverflow 这样的网站。

create table user (
  user_id integer primary key,
  name varchar(40) not null
);

create table category (
  category_id integer primary key,
  category varchar(40) not null
);

create table question (
  question_id integer primary key,
  question text,
  asked_by_id integer not null,
  category_id integer not null,
  foreign key asked_by_id references user(user_id),
  foreign key category_id references categor(category_id)
);

create table answer (
  answers_id     integer not null,
  answered_by_id integer not null,
  answer text,
  primary key (answers_id, answered_by_id), -- we allow one answer per person for a question
  foreign key answers_id references question(question_id),
  foreign key answered_by_id references user(user_id)
);

然后是查询:

select qu.name as asked_by,    question, category,
       au.name as answered_by, answer
from user qu                                       -- iterating through users
join question q on qu.user_id    = asked_by_id     -- joining them to their questions
join category c on c.category_id = q.category_id   -- find the category
join answer     on answers_id    = question_id     -- join by what answers this question
join user au    on au.user_id    = answered_by_id; -- looking up the user who answered it

(我没有在服务器上尝试过,所以可能有错别字。请随时纠正。)

于 2012-07-27T08:21:06.557 回答
0

好吧,这取决于你想做什么。用户可以发布问题吗?如果是,请链接它。如果不是,则链接没有意义,因为它会保持空白。

于 2012-07-27T07:55:09.457 回答
-1

无需更新多行,您只需将问题及其选项和答案转换为 json 并存储为单个字段。

于 2017-04-04T03:13:00.280 回答