开发者

SQL like button

So, I want to make an app that uses a voting system kinda like the like button on FB or the +1 button on Google. The one problem is that I have no idea how to design the database such that each of a large number of things has likes and dislikes from every user since it would be inefficient for each page that can be liked or dis开发者_如何学运维liked to have a like/dislike column for each user or for each user to have a like/dislike column for each page. How do I make the MySQL database process likes and dislikes?


Instead of creating multiple columns, you can use a link table:

user table

user_id | user_name
--------+----------
1       | alice
2       | bob
3       | charlie

page table

page_id | page_title
--------+--------------
1       | books
2       | food
3       | entertainment

user-page link table

user_id | page_id
--------+--------
1       | 1
1       | 2
2       | 3
3       | 3

The link table associates user_id values with page_id values. Note that one user can like more than one page, and one page can be liked by more than one user.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜