简体   繁体   English

PHP和MySQL中的评分系统

[英]Rating System in PHP and MySQL

If we look at the stackoverflow website we have votes. 如果我们查看stackoverflow网站,我们将获得投票。 But the question is what is the bestway to store who has voted and who has not. 但是问题是,存储投票的人和没有投票的人的最佳方法是什么。 Lets also simplify this even more and say that we can only vote Up, and we can only Remove the Up vote. 让我们进一步简化一下,说我们只能投票赞成,而我们只能删除投票赞成。

I was thinking having the table to be in such form 我本以为桌子是这样的形式

question - Id(INT) | userId(INT) | title(TEXT) | vote(INT) | ratedBy(TEXT)

Thre rest is self explanitory but ratedBy is a Comma Seperated Id values of the Users. 休息是自我解释,但ratingBy是用户的逗号分隔ID值。

I was thinking to read the ratedBy and compare it with the userId of the current logged in User. 我正在考虑读取ratingBy并将其与当前登录用户的userId进行比较。 If he dosent exist in the ratedBy he can vote Up, otherwise he can remove his vote. 如果他在额定值中存在,则可以投票,否则可以取消投票。 Which in turn will remove the value from ratedBy 依次将其从ratedBy中删除该值

I think to make another table "vote" is better. 我认为让另一个表“投票”会更好。 The relationship between users and votes is n to n, therefore a new table should be created. 用户和投票之间的关系是n到n,因此应创建一个新表。 It should be something like this: 应该是这样的:

question id (int) | user id (int) | permanent (bool) | timestamp (datetime)

Permanent field can be used to make votes stay after a given time, as SO does. 像SO一样,永久字段可用于在给定时间后保留选票。
Other fields may be added according to desired features. 可以根据期望的特征添加其他字段。 As each row will take at least 16B, you can have up to 250M rows in the table before the table uses 4GB (fat32 limit if there is one archive per table, which is the case for MyISAM and InnoDB). 由于每行至少需要16B,因此在表使用4GB之前,表中最多可以有250M行(如果每个表有一个存档,则为fat32限制,对于MyISAM和InnoDB就是这种情况)。
Also, as Matthew Scharley points out in a comment, don't load all votes at once into memory (as fetching all the table in a resultset). 另外,正如Matthew Scharley在评论中指出的那样,不要一次将所有投票加载到内存中(因为要获取结果集中的所有表格)。 You can always use LIMIT clause to narrow your query results. 您始终可以使用LIMIT子句来缩小查询结果的范围。

A new table: 新表:

Article ID | 商品编号| User ID | 用户名| Rating 评分

Where Article ID and User ID make up the composite key, and rating would be 1, indicating upvote, -1 for a downvote and 0 for a removed vote (or just remove the row). 由文章ID和用户ID组成组合键的等级为1,表示赞成,反对为-1,删除的表决为0(或仅删除行)。

I believe your design won't be able to scale for large numbers of voters. 我相信您的设计将无法满足大量选民的需求。 The typical thing to do is to create to tables 通常要做的是创建表

Table 1: question - Id(INT) | 表1:问题-Id(INT)| userId(INT) | userId(INT)| title(TEXT) 标题(文本)
Table 2: question - ID(INT) | 表2:问题-ID(INT)| vote(INT) | 投票(INT)| ratedBy(TEXT) ratedBy(文本)

Then you can count the votes with a query like this: 然后,您可以使用以下查询来计算票数:

SELECT t1.question_Id, t1.userId, t1.title, t2.sum(vote)
FROM table1 t1
LEFT JOIN table2 t2 ON t1.question_id = t2.question_id

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

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