我希望用户能够看到回答与他们相同的问题的其他用户。问题不是测验或测试格式。这些问题是为了了解一个人的个性,看看谁和他们一样。到目前为止,用户登录并被提示回答问题,一旦完成,他们将被带到一个屏幕,我希望显示谁也回答与他们相同的问题。
我有一个数据库(MySQL),我有一个“用户”表。我将“用户”表包装到REST服务中。我想知道我是否需要在我的数据库中有另一个包含问题和答案的表,并将其包装到我的REST服务中,或者如果没有必要那样。
提前谢谢!!!!
答案 0 :(得分:0)
当然,您需要将答案存储在数据库的表格中。否则,没有简单的方法可以找出,其他用户已经回答了什么。
假设您拥有以下MySQL数据库结构:
create table users(
id int not null auto_increment primary key,
...
);
create table questions(
id int not null auto_increment primary key,
...
);
create table answers(
user_id int not null references users(id),
question_id int not null references questions(id),
`option` int not null,
primary key (user_id, question_id)
);
(可能需要其他表和列)
在SQL中,查找具有ID为ID_of_your_user的用户的类似用户的查询看起来有点像这样:
select
u.id as similar_user_id,
count(*) as same_answer_count
from users u
join answers a on u.id = a.user_id
join answers a2 on a.question_id = a2.question_id
where a2.user_id = ID_of_your_user and
a2.`option` = a.`option` and
u.id != a2.user_id
group by u.id
having same_answer_count >= threshold
order by same_answer_count desc