I've been thinking about a liking system for my website, and I can't determine the best way to approach it. I'm using MySQL, and right now, I have an activities table, for posts of text, pictures, videos, etc, and a comments table, for comments. You can like both comments and activity posts, so how should I go about with the columns of the like table? I am open to suggestions of changing the whole schema all together for a more coherent design.
我一直在考虑为我的网站设计一个喜欢的系统,我无法确定最好的方法。我正在使用MySQL,现在,我有一个活动表,用于文本,图片,视频等的帖子,以及评论表。您可以同时阅读评论和活动帖子,那么我应该如何处理相似表格的列?我愿意接受改变整个架构的建议,以获得更加连贯的设计。
1 个解决方案
#1
13
Just make a table called likes
with a structure like this:
只需使用如下结构创建一个名为likes的表:
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`post_type` varchar(10) NOT NULL,
`post_id` int(11) NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
Then whenever someone likes an activity post, you would insert the ID of that user, the post_type
as activity, and then post_id
would be the post ID (obviously).
然后,每当有人喜欢活动帖子时,你会插入该用户的ID,post_type作为活动,然后post_id将是帖子ID(显然)。
If someone likes a comment, you would do the same thing but put comment for the post type instead.
如果有人喜欢评论,你会做同样的事情,但会对帖子类型进行评论。
Then to get the likes for an activity post:
然后获取活动帖子的喜欢:
SELECT * FROM `likes` WHERE post_id = 'id' AND post_type = 'activity'
You would just need to replace 'id'
with the ID variable.
你只需要用ID变量替换'id'。
The same would be for comments (except you would need to change the post_type
of course).
注释也是一样(除了你需要更改post_type)。
I hope this helps you get an idea on how to go about making a like system for your website :)
我希望这可以帮助您了解如何为您的网站制作类似的系统:)
#1
13
Just make a table called likes
with a structure like this:
只需使用如下结构创建一个名为likes的表:
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`post_type` varchar(10) NOT NULL,
`post_id` int(11) NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
Then whenever someone likes an activity post, you would insert the ID of that user, the post_type
as activity, and then post_id
would be the post ID (obviously).
然后,每当有人喜欢活动帖子时,你会插入该用户的ID,post_type作为活动,然后post_id将是帖子ID(显然)。
If someone likes a comment, you would do the same thing but put comment for the post type instead.
如果有人喜欢评论,你会做同样的事情,但会对帖子类型进行评论。
Then to get the likes for an activity post:
然后获取活动帖子的喜欢:
SELECT * FROM `likes` WHERE post_id = 'id' AND post_type = 'activity'
You would just need to replace 'id'
with the ID variable.
你只需要用ID变量替换'id'。
The same would be for comments (except you would need to change the post_type
of course).
注释也是一样(除了你需要更改post_type)。
I hope this helps you get an idea on how to go about making a like system for your website :)
我希望这可以帮助您了解如何为您的网站制作类似的系统:)