Mysql 嵌套选择

发布于 2024-10-05 01:02:13 字数 864 浏览 1 评论 0原文

继这个问题之后 last_question 使用此表

`id`, `bbs_id`, `user_id`, `like_dislike`
(15,   4,        2,         0),
(14,   4,        1,         0),
(13,   3,        1,         0),
(12,   2,        1,         1),
(11,   1,        2,         0),
(10,   1,        1,         1);

如何查看单个用户的内容喜欢还是不喜欢是?假设我想要一个包含所有喜欢和不喜欢的聚合表,并用另一列来表示用户 x 是否喜欢它。

这是我尝试过的查询,

$user_id = 1;


SELECT bbs_id,
     (SELECT like_dislike FROM bb_ratings WHERE user_id={$user_id}) AS thisUsersRating,
       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 bb_ratings
GROUP BY bbs_id

我想我在这里遇到的问题是,如何在这个特定行中而不是在所有行中引用 user_id = x 。 提前致谢 安德鲁

Following on from this question
last_question with this table

`id`, `bbs_id`, `user_id`, `like_dislike`
(15,   4,        2,         0),
(14,   4,        1,         0),
(13,   3,        1,         0),
(12,   2,        1,         1),
(11,   1,        2,         0),
(10,   1,        1,         1);

How can I see what an individual users like or dislike was? Lets say I wanted to have an aggregate table of all the likes and dislikes with another column for whether user x liked it.

This is the query that I have tried

$user_id = 1;


SELECT bbs_id,
     (SELECT like_dislike FROM bb_ratings WHERE user_id={$user_id}) AS thisUsersRating,
       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 bb_ratings
GROUP BY bbs_id

I guess the problem I am running into here is, how do you refer to user_id = x in this particular row, not in all the rows.
Thanks in Advance
Andrew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

杀お生予夺 2024-10-12 01:02:14

我已经补充了上一个问题的答案,请先参考一下,以便理解。

您无法仅通过对 bb_ ratings 进行分组和破解来获取它。您得到 Null 是因为您正在考虑网格,而不是关系集(这是关系模型的中心概念)。

  1. 在决定转到哪个表来为查询提供服务之前,您需要确定结果集的结构。

  2. 然后使用 WHERE 子句约束它(哪些行)。

  3. 然后找出从哪里(哪些表)获取列。要么联接到更多表,并在 WHERE 子句上进行更多工作;或标量子查询,与外部查询相关。

你不清楚自己想要什么。看起来您想要与上一个问题相同的报告,再加上给定用户投票的列。对我来说,结果集的结构是公告列表。好吧,我可以从 bulletin 中获取该信息,无需转到 bulletin_like 然后必须对其进行分组。

如果您从集合的角度思考,这非常简单,无需使用物化视图或“嵌套”查询来跳过麻烦:

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,
    (SELECT COUNT(like) 
        FROM  bulletin_like bl 
        WHERE bl.bulletin_id = b.bulletin_id 
        AND   bl.user_id = {$user_d}
        AND   like = 1
        ) AS your_vote
    FROM bulletin b

对评论的回应

我觉得你所说的对我处理 SQL 的方式非常重要

  1. 是的,绝对如此。如果您愿意预先学习正确的东西,它将:

    • 省去你以后遇到的各种问题
    • 让您的查询更加高效
    • 让您更快地编码
  2. 现在,忘记使用结果集作为表(慢得多)和临时表(如果您的数据库是规范化的,则绝对不需要)。直接查询表会更好。你需要学习各种科目,例如关系模型;如何使用SQL;如何使用 SQL 以避免出现问题;等等。我愿意帮助你,并陪你一段时间,但我需要知道你是否愿意。这将需要一些来回。这个网站上有一些噪音,所以我会忽略其他评论(直到最后)并只回复您的评论。

    • 停止使用GROUP BY,它严重阻碍你对SQL的理解。如果您在不使用 GROUP BY 的情况下无法获得所需的报告,请提出问题。
      .
  3. 这个发布的问题。让我知道您在哪一点迷路了,我将从那时起提供更多详细信息。

    • 对于这个问题,您需要一个带有点赞的公告列表;不喜欢;并且这个用户喜欢。这是正确的吗?您尝试过我提供的代码吗?
      .
  4. 看了链接的问题。一团糟,没有人解决更深层次的问题;他们表面上、孤立地回答了这个问题。现在你已经有了答案,但你不明白。这是一种非常缓慢的进步方式。

I have added an answer to the previous question, please refer to that first, for understanding.

You can't get it from bb_ratings alone by GROUPing it and hacking it. You get Null because you are thinking in terms of a grid, not relational sets (that is the central concept of the Relational Model).

  1. Before you decide which table(s) to go to, to service your query, you need to decide what you want for the structure of your result set.

  2. Then constraint it (which rows) with the WHERE clause.

  3. Then figure out where (what tables) to get the columns from. Either joins to more tables, and more work on the WHERE clause; or scalar subqueries, correlated to the outer query.

You are not clear about what you want. It looks like you want the same report as the previous question, plus a column for the given users vote. To me, the structure of your result set is a list of bulletins. Well, I can get that from bulletin, no need to go to bulletin_like and then have to GROUP that.

If you think in terms of sets, it is very easy, no need to jump through hoops with materialised views or "nested" queries:

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,
    (SELECT COUNT(like) 
        FROM  bulletin_like bl 
        WHERE bl.bulletin_id = b.bulletin_id 
        AND   bl.user_id = {$user_d}
        AND   like = 1
        ) AS your_vote
    FROM bulletin b

Responses to Comments

I have the feeling that what you are saying is very important for how I approach SQL

  1. Yes, absolutely. If you are willing to learn the right stuff up front, it will:

    • save you all kinds of problems later
    • make your queries more efficient
    • allow you to code faster
      .
  2. For now, forget about using result sets as tables (much slower), and temp tables (definitely not required if your database is Normalised). You are much better off querying the tables directly. You need to learn various subjects such as the Relational model; how to use SQL; how not to use SQL so as to avoid problems; etc. I am willing to help you, and stay with you for a while, but I need to know you are willing. It will take a bit of back-and-forth. There is a bit of noise on this site, so I will ignore other comments (until the end) and respond only to yours.

    • stop using GROUP BY, it is seriously hindering your understanding of SQL. If you can't get the report you want without using GROUP BY, ask a question.
      .
  3. This posted question. Let me know at which point you got lost, and I will provide more detail from that point forward.

    • For this question, you want a list of bulletins, with likes; dislikes; and this users likes. Is that correct ? Have you tried the code I provided ?
      .
  4. Looked at the linked question. It is a mess, and no one has addressed the deeper problem; they have answered the problem on the surface, in isolation. You now have an answer but you do not understand it. That is a very slow way to progress.
以酷 2024-10-12 01:02:14

$user_id = 1

SELECT bbs_id,
       sum(CASE WHEN user_id = {$user_id} THEN like_dislike END) AS thisUsersRating,
       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 bb_ratings
GROUP BY bbs_id

给我

bbs_id     thisUsersRating  likes         dislikes
    (1,         '1',            '1',             '1'),
    (2,         '1',            '1',             '0'),
    (3,         '0',            '0',             '1'),
    (4,         '0',            '0',             '2');

如果我将 user_id 更改为 4,那么所有 thisUsersRating 字段都会得到 null。

$user_id = 1

SELECT bbs_id,
       sum(CASE WHEN user_id = {$user_id} THEN like_dislike END) AS thisUsersRating,
       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 bb_ratings
GROUP BY bbs_id

Gives me

bbs_id     thisUsersRating  likes         dislikes
    (1,         '1',            '1',             '1'),
    (2,         '1',            '1',             '0'),
    (3,         '0',            '0',             '1'),
    (4,         '0',            '0',             '2');

If I change the user_id to 4 then I get null for all thisUsersRating fields.

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