Mysql 跟踪用户的喜好和厌恶

发布于 2024-10-04 02:50:43 字数 688 浏览 4 评论 0原文

我正在创建一个公告板应用程序。每个公告都可以被网站的用户喜欢或不喜欢。为了跟踪喜欢和不喜欢,我创建了以下数据库表

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅一夜美梦 2024-10-11 02:50:43

您可以通过聚合查询来完成此操作,在单个 like_dislike 列上使用相反的条件(我假设该列中的“1”表示“喜欢”)。

SELECT bulletin_id, 
       SUM(CASE WHEN like_dislike = 1 THEN 1 ELSE 0 END) AS likes, 
       SUM(CASE WHEN like_dislike = 0 THEN 1 ELSE 0 END) AS dislikes
FROM bulletins_ld
GROUP BY bulletin_id

更新:根据下面评论中的讨论,喜欢/不喜欢列可以规范化到自己的表中,如下所示(故意愚蠢的示例......):

CREATE TABLE how_user_feels(
    feeling_id INT,
    feeling_desc VARCHAR(20)
)

INSERT INTO how_user_feels(feeling_id, feeling_desc) VALUES
(0, 'Undecided'),
(1, 'Likes It'),
(2, 'Could Do Without It')

公告表中的 Likes_Dislikes 列是然后用外键feeling_id替换,默认为0。假设当用户第一次查看公告时,您在此表中输入一条记录,默认情况下将其设置为“未决定”,然后更新他们对公告进行投票时的记录。您可以像这样查询结果:

SELECT bulletin_id, 
       SUM(CASE WHEN feelings_id = 1 THEN 1 ELSE 0 END) AS likes, 
       SUM(CASE WHEN feelings_id = 2 THEN 1 ELSE 0 END) AS dislikes,
       SUM(CASE WHEN feelings_id = 0 THEN 1 ELSE 0 END) AS doesnt_seem_to_care
FROM bulletins_ld b
INNER JOIN how_user_feels h ON b.feeling_id = h.feeling_id
GROUP BY bulletin_id

请记住,这只是一种方法,可能对您的情况没有用。但是,如果您决定更改扩展用户表达对公告的感受的模型(例如五星级评级系统),那么您无需更改数据库架构 - 只需更改 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').

SELECT bulletin_id, 
       SUM(CASE WHEN like_dislike = 1 THEN 1 ELSE 0 END) AS likes, 
       SUM(CASE WHEN like_dislike = 0 THEN 1 ELSE 0 END) AS dislikes
FROM bulletins_ld
GROUP BY bulletin_id

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...):

CREATE TABLE how_user_feels(
    feeling_id INT,
    feeling_desc VARCHAR(20)
)

INSERT INTO how_user_feels(feeling_id, feeling_desc) VALUES
(0, 'Undecided'),
(1, 'Likes It'),
(2, 'Could Do Without It')

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:

SELECT bulletin_id, 
       SUM(CASE WHEN feelings_id = 1 THEN 1 ELSE 0 END) AS likes, 
       SUM(CASE WHEN feelings_id = 2 THEN 1 ELSE 0 END) AS dislikes,
       SUM(CASE WHEN feelings_id = 0 THEN 1 ELSE 0 END) AS doesnt_seem_to_care
FROM bulletins_ld b
INNER JOIN how_user_feels h ON b.feeling_id = h.feeling_id
GROUP BY bulletin_id

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.

旧城烟雨 2024-10-11 02:50:43
  1. 郑重声明,还有另一种方法可以获取相同的结果集,更快或更慢取决于平台。这使用标量子查询而不是外部查询上的聚合。这个想法是,如果你从集合的角度思考,这应该会更容易。或者就维度事实而言。

    首先我们必须理顺你们的名字。让我们将您的“表”命名为 bulletin_like 和主公告表 bulletinbulletin_id 对于它们中的任何一个来说都是一个非常愚蠢的名称,更多的是列名称)。只需调用布尔列 like (如果为 1,like 为 true;如果为 0,like 为 false;这就是 boolean方法)。名称使用单数形式。

    选择名称 AS 公告, 
        (选择 COUNT(喜欢) 
            来自Bulletin_like bl 
            其中 bl.bulletin_id = b.bulletin_id 
            喜欢 = 1
            )就像,
        (选择 COUNT(喜欢) 
            来自Bulletin_like bl 
            其中 bl.bulletin_id = b.bulletin_id 
            喜欢 = 0
            )作为不喜欢
    FROMBulletin b

  2. 您要求标准化标签。该bulletin_like“表”未规范化。去掉Idiot 列,它除了冗余列和附加索引之外没有任何用途。 PK 为(bulletin_id, user_id)

    除非您希望用户在每个公告的每个海报上发布多个喜欢和不喜欢的内容。

  1. 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 table bulletin (bulletin_id is a very silly name for either of them, that is more of a column name). And just call the boolean column like (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.

    SELECT name AS bulletin, 
        (SELECT COUNT(like) 
            FROM  bulletin_like bl 
            WHERE bl.bulletin_id = b.bulletin_id 
            AND   like = 1
            ) AS like,
        (SELECT COUNT(like) 
            FROM  bulletin_like bl 
            WHERE bl.bulletin_id = b.bulletin_id 
            AND   like = 0
            ) AS dislike
    FROM bulletin b

  2. You asked for the Normalisation tag,. That bulletin_like "table" is not Normalised. Get rid of theIdiot 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.

_蜘蛛 2024-10-11 02:50:43

这个查询对我有用:

SELECT bulletin_id, 
    sum(like_dislike) AS likes, 
    sum((1-like_dislike)) AS dislikes 
FROM bulletins_ld 
    GROUP BY (bulletin_id);

假设 likedislike = 1 表示“喜欢它”,likedislike = 0 表示“不喜欢它”。

ID    LIKES    DISLIKES
1     2    1
2     0    1

This query worked for me:

SELECT bulletin_id, 
    sum(like_dislike) AS likes, 
    sum((1-like_dislike)) AS dislikes 
FROM bulletins_ld 
    GROUP BY (bulletin_id);

This assumes that likedislike = 1 means "LIKE IT" and likedislike = 0 means "DOES NOT LIKE IT".

ID    LIKES    DISLIKES
1     2    1
2     0    1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文