“照片评级”数据库设计

时间:2015-08-21 11:52:18

标签: database database-design

我正在创建一个数据库,用于保存用户的照片,并将返回每日最喜欢的照片<还为每个月,每年> 例如:我创建了一张照片A,第一天,我有100个喜欢,第二天,我有200个喜欢,我有300个喜欢,所以我必须每天存储喜欢的价值。 我认为一个结构喜欢这个:

Table Photo[photo_id, user_id, photo_url]

Table Day_Rating[id, photo_id, like_count, date]
//Photo A can have many records in this table

Table Month_Rating[id, photo_id, like_count, month] //month is: 1,2,3,4..12
//Photo A can have many records in this table

因此,当用户喜欢照片时,我们会在day_Rating和Month_Rating表的like_count字段上增加1。 因此,为了获得每日评分,我们将在Day_Rating表上获得与like_count最相似的评分,与月评分相同。 您如何看待这种方法?如果有更好的话,你能指出我最好的做法吗?  感谢

1 个答案:

答案 0 :(得分:2)

只需两张桌子即可完成所有操作:

photo
* id
* posted_by_user_id
* url

rating
* id
* photo_id
* rated_by_user_id
* rated_at
* rate

然后让数据库引擎执行聚合。此外,单独存储每个评级将允许您执行“每个用户只有一个评级”等规则,并允许用户撤销他们的投票。