Mysql 跟踪用户的喜好和厌恶
我正在创建一个公告板应用程序。每个公告都可以被网站的用户喜欢或不喜欢。为了跟踪喜欢和不喜欢,我创建了以下数据库表
id user_id bulletin_id like_dislike
1 1 1 1
2 1 2 0
3 3 1 1
4 2 1 0
在 like_dislike 列中 1 表示“喜欢”,0 表示“不喜欢” 我知道怎么问。 - 公告 1 被点赞了多少次 (2) - 公告 1 被不喜欢了多少次 (1)
但是我如何进行查询以同时提出这两个问题?也就是说,公告 1 被喜欢和不喜欢的次数
liked disliked
2 1
我已经尝试过该查询
SELECT count(like_dislike) AS likes, count(like_dislike) AS dislikes FROM bulletins_ld
where bulletins_id = 1 AND likes = 1 AND dislikes = 0
,但我得到的只是两次,这并不奇怪。
我能想到的唯一解决方案是有一个单独的喜欢和不喜欢的专栏
I am creating a bulletin board application. Each bulletin can be liked or disliked by users of the site.To keep track of the likes and dislikes I have created the following database table
id user_id bulletin_id like_dislike
1 1 1 1
2 1 2 0
3 3 1 1
4 2 1 0
In the like_dislike column 1 means 'Like It', 0 means 'Don't like it'
I know how to ask.
- How many times was bulletin 1 liked (2)
- How many times was bulletin 1 disliked (1)
But How do I do a query to ask those two questions at the same time? That is, how many times was bulletin 1 liked and disliked
liked disliked
2 1
I have tried the query
SELECT count(like_dislike) AS likes, count(like_dislike) AS dislikes FROM bulletins_ld
where bulletins_id = 1 AND likes = 1 AND dislikes = 0
but all I get is two twice which is not surprising.
The only solution I can think of is having a separate like and dislike column
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过聚合查询来完成此操作,在单个 like_dislike 列上使用相反的条件(我假设该列中的“1”表示“喜欢”)。
更新:根据下面评论中的讨论,喜欢/不喜欢列可以规范化到自己的表中,如下所示(故意愚蠢的示例......):
公告表中的 Likes_Dislikes 列是然后用外键
feeling_id
替换,默认为0。假设当用户第一次查看公告时,您在此表中输入一条记录,默认情况下将其设置为“未决定”,然后更新他们对公告进行投票时的记录。您可以像这样查询结果:请记住,这只是一种方法,可能对您的情况没有用。但是,如果您决定更改或扩展用户表达对公告的感受的模型(例如五星级评级系统),那么您无需更改数据库架构 - 只需更改
how_user_feels
表中的记录以及关联的查询。You can do this with an aggregate query, using opposing conditions on the single like_dislike column (I am assuming below that a '1' in that column means 'liked').
Update: As per the discussion in the comments below, the like/dislike column could be normalized into its own table, like so (example deliberately silly...):
The Likes_Dislikes column in the Bulletin table is then replaced by the foreign key
feeling_id
, with a default to 0. Let's say that you then enter a record in this table when a user first views a bulletin, making them "Undecided" by default, and update that record when they vote on the bulletin. You could query the results like so:Keep in mind, this is just one approach, and may not be useful in your case. But if you ever decided to change or expand the model by which a user expresses their feelings for a bulletin, say to a five-star rating system, you could do that without changing the database schema - just alter the records in the
how_user_feels
table, and the associated queries.郑重声明,还有另一种方法可以获取相同的结果集,更快或更慢取决于平台。这使用标量子查询而不是外部查询上的聚合。这个想法是,如果你从集合的角度思考,这应该会更容易。或者就维度事实而言。
首先我们必须理顺你们的名字。让我们将您的“表”命名为
bulletin_like
和主公告表bulletin
(bulletin_id
对于它们中的任何一个来说都是一个非常愚蠢的名称,更多的是列名称)。只需调用布尔列like
(如果为 1,like
为 true;如果为 0,like
为 false;这就是 boolean方法)。名称使用单数形式。您要求标准化标签。该
bulletin_like
“表”未规范化。去掉Id
iot 列,它除了冗余列和附加索引之外没有任何用途。 PK 为(bulletin_id, user_id)
。除非您希望用户在每个公告的每个海报上发布多个喜欢和不喜欢的内容。
For the record, there is another way to obtain the same result set, whether it is faster or slower depends on the platform. This uses a scalar subquery instead of an aggregate on the outer query. The idea is, this is supposed to be easier if you think in terms of sets. Or in terms of Dimension-Facts.
First we have to straighten out your names. Let's call your "table"
bulletin_like
and the main bulletin tablebulletin
(bulletin_id
is a very silly name for either of them, that is more of a column name). And just call the boolean columnlike
(if it is 1,like
is true; if it is 0,like
is false; that's what boolean means). Use the singular form for names.You asked for the Normalisation tag,. That
bulletin_like
"table" is not Normalised. Get rid of theId
iot column, it serves no purpose other than a redundant column and an additional index. The PK is(bulletin_id, user_id)
.Unless you want users to post multiple likes and dislikes per poster per bulletin.
这个查询对我有用:
假设 likedislike = 1 表示“喜欢它”,likedislike = 0 表示“不喜欢它”。
This query worked for me:
This assumes that likedislike = 1 means "LIKE IT" and likedislike = 0 means "DOES NOT LIKE IT".