php mysql 5星评级结构

发布于 2024-12-29 23:00:46 字数 405 浏览 2 评论 0原文

好吧,我知道这不是一个独特的概念。然而,我发现的示例和教程并没有真正涵盖太多内容,它们都是关于这就是您所做的,上传并完成。这在很大程度上是可以的。不过我想从头开始构建一个。

我所拥有的是对产品进行投票的用户,我正在尝试找出如何正确地将表格组合在一起以支持这一点。所以我有我的用户表、我的产品表,我想从中创建 2 个新表。其中一个实际上是每个产品的投票,每个产品投票排名将有一个唯一的 ID,这样我就可以有第二个表,其中包含评论和投票。我可以在大桌子上创建两者的组合,是的..但我预见随着桌子的增长,未来会出现问题。所以我宁愿将两者分开。我想做的另一件事是密切关注 10 个不同的排名选择。 1/2 为最低票数,5 票为最高票数。那么构建一个表的最佳方法是什么,至少可以存储每个产品的评级。

除此之外,输入数字的最佳方式是什么?小数?整数?我如何计算 5 星级概念。

Ok, I know this isn't a unique concept. However the examples and tutorials I find don't really cover much they are all about this is what you do, upload it and done. Which to many extents is ok. However I want to build one from scratch.

What I have is a users who are voting on products and I am trying to figure out how to put the tables together properly to support this. So I have my users table, my product table, and from that I want to create 2 new tables. One thats actually the votes per product which will have a unique ID per product vote ranking so I can then have a second table that would have comments along with the votes. I could create on large table for the combination of the 2, yes.. but I foresee issues with that down the road as the table grows. So I would rather keep the 2 seperate. Another thing I want to do is keep tabs on the 10 different rank choices. 1/2 being the lowest 5 being the highest with total over all votes. So whats the best way to construct a table for at the least the rating storage per product.

That aside whats the best way to put the numbers in? decimals? whole numbers? how would I calculate the 5 star concept.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期以后 2025-01-05 23:00:46

三个表(我假设您已经拥有其中两个):

users (id, etc...)
products (id, etc...)

votes (userId, productId, rating, comments)

rating 可以是 SMALLINT 类型。这为您提供了 0-255 的范围,您可以将其视为用户的分数“乘以十”。例如:4 颗星 = 40,0.5 颗星 = 5。除非您预见到用户想要给某些东西 3.1415 颗星,否则这应该足够了,而且比 float 列的内存效率更高。

然后,当您想找到平均分数时:

SELECT AVG(rating) FROM votes WHERE productId = xxx

我认为您不应该对这样的表结构有任何特殊问题。如果您非常偏执,那么是的,您可以为评论创建一个单独的表,但是,这将是过早优化的教科书定义。

Three tables (two of which I assume you already have):

users (id, etc...)
products (id, etc...)

votes (userId, productId, rating, comments)

rating can be a SMALLINT type. This gives you a range of 0-255, and you could treat this as the user's score "times ten". eg: 4 stars = 40, 0.5 stars = 5. Unless you foresee users wanting to give something 3.1415 stars, that should be quite sufficient, plus more memory efficient than a float column.

Then, when you want to find the average score:

SELECT AVG(rating) FROM votes WHERE productId = xxx

I don't think that you should have any particular problems with a table structure like that. If you were being super-amazingly-paranoid, then yes, you could create a separate table for the comments, however, that would be the textbook definition of premature optimisation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文