dougu2006 2009-05-01 02:02
浏览 17
已采纳

评论投票数据库结构的最佳实践

I'm working on a PHP app that has several objects that can be commented on. Each comment can be voted on, with users being able to give it +1 or -1 (like Digg or Reddit). Right now I'm planning on having a 'votes' table that has carries user_id and their vote info, which seems to work fine.

The thing is, each object has hundreds of comments that are stored in a separate comments table. After I load the comments, I'm having to tally the votes and then individually check each vote against the user to make sure they can only vote once. This works but just seems really database intensive - a lot of queries for just the comments.

Is there a simpler method of doing this that is less DB intensive? Is my current database structure the best way to go?

To be clearer about current database structure:

Comments table:

  • user_id
  • object_id
  • total_votes

Votes table:

  • comment_id
  • user_id
  • vote

End Goal:

  • Allow user to vote only once on each comment with least # of MySQL queries (each object has multiple comments)
  • 写回答

3条回答 默认 最新

  • doucong3048 2009-05-01 02:12
    关注

    To make sure that each voter votes only once, design your Votes table with these fields—CommentID, UserID, VoteValue. Make CommentID and UserID the primary key, which will make sure that one user gets only one vote. Then, to query the votes for a comment, do something like this:

    SELECT SUM(VoteValue)
    FROM Votes
    WHERE CommentID = ?
    

    Does that help?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真