简体   繁体   English

MySQL中的简单评级系统

[英]Simple rating system in MySQL

Let's say I am running a small vote counter product. 假设我正在运行一个小投票计数器产品。 The values are stored in the database "content", with a row containing the amount of votes ("count"), I also have a row called "rank", how would I autosum these values so that I don't have to iterate through every single one in PHP, causing the script to slow down tremendously? 这些值存储在数据库“content”中,其中一行包含投票数(“count”),我还有一行称为“rank”,我如何自动调整这些值以便我不必迭代通过PHP中的每一个,导致脚本大幅减速?

If you have a structure like this in a table 如果你在表格中有这样的结构

item with fields: name, id, count, ... 带字段的项目:name,id,count,...

you can simply do: 你可以简单地做:

Up vote in the item with id = $id 在id = $ id的项目中进行投票

UPDATE item SET count = count + 1 WHERE id='$id';

Down vote in the item with id = $id 在id = $ id的项目中向下投票

UPDATE item SET count = count - 1 WHERE id='$id';

count will store the total number of votes. count将存储总票数。

If you want to check for avoiding more than one vote per user/ip you should store each individual vote in another table and check this aditional table for non repeated votes before sending the previous queries. 如果要检查每个用户/ ip是否要避免多次投票,则应将每个投票存储在另一个表中,并在发送之前的查询之前检查此附加表是否有非重复投票。

Something like this (depending on your exact table structure) should work: 这样的事情(取决于你确切的表结构)应该工作:

UPDATE item SET rating = 
   (SELECT count(*) FROM rates WHERE type 'positive') - 
   (SELECT count(*) FROM rates WHERE type 'negative');

For more information have a look at the MySQL docs . 有关更多信息,请查看MySQL文档

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

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