SQL database structure for Like and DisLike [closed]

戏子无情 提交于 2019-12-03 21:53:13

This is a simple schema you could use. isLike is a boolean, true if it's a like, false if it's a dislike.

To query the total likesdislikes by topic

    Select Count(*), Topic,isLike FROM LikesDislikes GROUP BY Topic,isLike

For all of a user's liked topics

   SELECT topic FROM likesdislikes WHERE userName = 'user' AND isLike = true;

And so forth.

If you have a table of Topics and a table of Users, you would add a table of Likes which links to both. Something like this:

User
----------
ID (int, PK)
Name (string)
etc...

Topic
----------
ID (int, PK)
Title (string)
etc...

Like
----------
ID (int, PK)
UserID (int, FK to User.ID)
TopicID (int, FK to Topic.ID)
IsLike (boolean)
etc...

So any time a user "likes" something you add a record to that table setting IsLike to true. If they "dislike" something then you add a record to that table setting IsLike to false. You can change around the terminology/names/types/etc. but the general idea is the same. A "like" becomes a linking record between a User and a Topic.

So when displaying the topic, you just select the count of records from the linking table which are associated with that topic. And when displaying a user you select the records from the linking table which are associated with that user.

You could have three SQL tables:

Topics Ratings Users

You could then link the Users table via the Ratings table to the Topics table and do a query:

select * from Topics where RatingsUserID = UsersUserID

(pseudo code)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!