Android应用程序:如何根据用户的相似答案显示用户



我希望用户看到回答问题的其他用户与他们相同。这些问题不是测验或测试格式。问题是那些了解一个人的个性,看看谁和他们一样。到目前为止,用户登录并被提示回答问题,完成后,他们将被带到一个屏幕,我想显示谁也回答了与他们相同的问题。

我有一个数据库(MySQL(,其中有一个"用户"表。我将"用户"表包装到 REST 服务中。我想知道我是否需要在我的数据库中有另一个表来包含问题和答案,并将其包装到我的 REST 服务中,或者是否没有必要拥有它。

提前谢谢你!!!

当然,您需要将答案存储在数据库的表格中。否则,将没有简单的方法来找出其他用户的答案。

假设您的 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

最新更新