[英]How would I implement a star rating system into my Database design?
I am currently making an Android app where I want to allow users to rate posts, posted by other users.我目前正在制作一个 Android 应用程序,我希望允许用户对其他用户发布的帖子进行评分。
This is my current design, however, would I include Rating as a separate table or included in the Media upload table?这是我当前的设计,但是,我会将评级作为单独的表包含还是包含在媒体上传表中?
If it is a separate table would it be something like this?如果它是一个单独的表,它会是这样的吗?
Rating table: (Rating, PostID, Rating_Count, Average_Rating)评分表:(Rating、PostID、Rating_Count、Average_Rating)
If anything else looks wrong with my design that would also be appreciated!如果我的设计还有什么问题,那也将不胜感激!
It all depends on how you want the ratings to work, who gets to up-vote or down-vote, whether you track everyone that votes to keep from having multiple votes from the same person, etc. 这一切都取决于你希望评级如何运作,谁进行投票或投票,是否跟踪每个人投票以避免同一个人多次投票等。
This is what I would do: add a unique numeric (long?) RatingID
field for each vote, link the PostID
field to the Comments table, add a Rating
(integer) field that is limited to values from 0 to 5 (or whatever range you prefer), and then calculate the average from all votes cast (delete the Rating_Count
and Average_Rating
fields). 这是我会做:添加一个唯一的数字(长?)
RatingID
场每票,链接PostID
字段comments表,增加一个Rating
(整数)字段的值限制为0至5(或任何范围你喜欢),然后计算从投所有选票(删除平均Rating_Count
和Average_Rating
领域)。 You could define a view to calculate the average ratings for each post to use for your routine for determining how to display it next to the Post. 您可以定义一个视图来计算每个帖子的平均评级,以用于例程,以确定如何在Post旁边显示它。
Also, I would use an ID for each user, not use their names. 另外,我会为每个用户使用ID,而不是使用他们的名字。
So my table would look like this: 所以我的表看起来像这样:
RatingID, PostID, UserID, Rating
Also, to keep users from voting multiple times, the table would not allow multiple entries for the same PostID
and UserID
. 此外,为了防止用户多次投票,该表不允许同一
PostID
和UserID
多个条目。
It depends on how you want to store the Rating. 这取决于您希望如何存储评级。
If you want to track each individual rating. 如果您想跟踪每个评级。 You will need a Rating table with the following:
您将需要一个评级表,其中包含以下内容:
Column
---------
RatingID (PK)
Username (FK) (UserDetailsID if you change the primary key of the UserDetails table)
PostID (FK)
RatingScore
DateRated
In the app, you would then pull back every RankScore
with that PostID
and do the calculation based off a count of said PostID
. 在应用程序中,您将使用该
PostID
撤回每个RankScore
,并根据所述PostID
的计数进行PostID
。
I would recommend the above method because it will allow you do more things with the Ratings in the future. 我会推荐上述方法,因为它可以让你在将来使用评级做更多的事情。 Plus, you have a good structure to follow in place with your
Comments
table. 另外,您有一个很好的结构可以使用您的
Comments
表。 ex. 恩。 Show who Rated what and at what score.
显示谁评分什么和得分。
If you want to attach it to the Post
table, another table is not needed. 如果要将其附加到
Post
表,则不需要另一个表。 You would simply add Rating_Count
and Average_Rating
to the table. 你会简单地添加
Rating_Count
和Average_Rating
表。 In your app, you would than have to perform an update every time the Post is rated. 在您的应用中,您不必每次评价帖子时都要执行更新。 You would have to pull the
Rating_Count
and Average_Rating
, increment the Rating_Count
by one, recalculate the Average_Rating
and perform the update. 你将不得不拉
Rating_Count
和Average_Rating
,递增Rating_Count
接一个,重新计算Average_Rating
和执行更新。
My second suggestion is less flexible if you ever want to enhance your Rating setup though. 如果您想提高评分设置,我的第二个建议就不那么灵活了。
you can use stretcher like this你可以像这样使用担架
enter code here
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->ipAddress('ip')->nullable();
$table->integer('star');
$table->string('type');
$table->string('title');
$table->string('average')->nullable()->default(0);
$table->integer('count_star')->default(0)->nullable();
$table->float('star_avg')->default(0)->nullable();
$table->float('unstar_avg')->default(0)->nullable();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.