简体   繁体   English

将 mysql 数据库中的用户输入与 php 进行比较

[英]comparing user inputs in mysql database with php

I have a table that stores user choices to lots of questions.我有一张表,可以存储用户对很多问题的选择。
Choices are A,B or C.选项为 A、B 或 C。 ( or 0,1,2 but limited to 3 same values for every question) So the table looks like this: (或 0,1,2,但每个问题限制为 3 个相同的值)所以表格如下所示:

question user choice
1        1    A
1        2    A
1        3    B
2        1    C
2        2    A
2        3    B
3        1    B
3        2    C
3        3    B
.        .    .
.        .    .
.        .    .
N        N    N

What i want to do is to compare choices and group users with same choices like last.fm neighbours.我想要做的是比较选择和组用户具有相同的选择,如 last.fm 邻居。

I can compare one user against another user with IN and NOT IN queries, or selecting all rows and comparing with php's array functions.我可以使用 IN 和 NOT IN 查询将一个用户与另一个用户进行比较,或者选择所有行并与 php 的数组函数进行比较。 But as the database grows this approach will not be feasible and this also compares only two users.但随着数据库的增长,这种方法将不可行,而且这也只比较两个用户。

Maybe hashing user's choices in a way that i couldn't imagine can help.也许以我无法想象的方式散列用户的选择会有所帮助。

SELECT a.user, b.user, COUNT(*) AS common
FROM Choice a
LEFT JOIN Choice b 
       ON a.user != b.user 
      AND a.question = b.question 
      AND a.choice = b.choice 
GROUB BY a.user, b.user

Will return for two users how many questions they have answered the same way.将为两个用户返回他们以相同方式回答了多少个问题。 Maybe this helps.也许这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM