Mysql 嵌套选择
继这个问题之后 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经补充了上一个问题的答案,请先参考一下,以便理解。
您无法仅通过对 bb_ ratings 进行分组和破解来获取它。您得到 Null 是因为您正在考虑网格,而不是关系集(这是关系模型的中心概念)。
在决定转到哪个表来为查询提供服务之前,您需要确定结果集的结构。
然后使用
WHERE
子句约束它(哪些行)。然后找出从哪里(哪些表)获取列。要么联接到更多表,并在
WHERE
子句上进行更多工作;或标量子查询,与外部查询相关。你不清楚自己想要什么。看起来您想要与上一个问题相同的报告,再加上给定用户投票的列。对我来说,结果集的结构是公告列表。好吧,我可以从
bulletin
中获取该信息,无需转到bulletin_like
然后必须对其进行分组。如果您从集合的角度思考,这非常简单,无需使用物化视图或“嵌套”查询来跳过麻烦:
对评论的回应
我觉得你所说的对我处理 SQL 的方式非常重要
是的,绝对如此。如果您愿意预先学习正确的东西,它将:
。
现在,忘记使用结果集作为表(慢得多)和临时表(如果您的数据库是规范化的,则绝对不需要)。直接查询表会更好。你需要学习各种科目,例如关系模型;如何使用SQL;如何不使用 SQL 以避免出现问题;等等。我愿意帮助你,并陪你一段时间,但我需要知道你是否愿意。这将需要一些来回。这个网站上有一些噪音,所以我会忽略其他评论(直到最后)并只回复您的评论。
GROUP BY
,它严重阻碍你对SQL的理解。如果您在不使用GROUP BY
的情况下无法获得所需的报告,请提出问题。.
这个发布的问题。让我知道您在哪一点迷路了,我将从那时起提供更多详细信息。
.
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).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.
Then constraint it (which rows) with the
WHERE
clause.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 tobulletin_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:
Responses to Comments
I have the feeling that what you are saying is very important for how I approach SQL
Yes, absolutely. If you are willing to learn the right stuff up front, it will:
.
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.
GROUP BY
, it is seriously hindering your understanding of SQL. If you can't get the report you want without usingGROUP BY
, ask a question..
This posted question. Let me know at which point you got lost, and I will provide more detail from that point forward.
.
$user_id = 1
给我
如果我将 user_id 更改为 4,那么所有 thisUsersRating 字段都会得到 null。
$user_id = 1
Gives me
If I change the user_id to 4 then I get null for all thisUsersRating fields.