LEFT JOIN 的奇怪 MySQL 行为

发布于 2024-11-28 01:24:53 字数 1338 浏览 0 评论 0原文

我有一个奇怪的问题。我有一个用户表,其中每个用户都有一个唯一的用户 ID。用户可以发布评论(提示),这些评论会保存到不同的表中,每个评论行也有一个唯一的 ID,并且还包含发布该评论的用户的用户 ID。最后,其他用户可以对评论进行投票。投票与投票者的用户 ID 以及被投票评论的评论 ID 一起存储在另一个表中(无唯一 ID)。

我用赞成票数减去反对票数来计算一个人的评分,所以 4 个赞成票和 2 个反对票就会得到 2 分,很简单。我想要一个可以按人员评级排序的成员列表,但我遇到了一些问题。

我使用 COUNT() 和 CASE 来确定赞成票和反对票的数量:

(COUNT(CASE WHEN V.vote = 1 THEN 1 ELSE NULL END)-COUNT(CASE WHEN V.vote = 0 THEN 1 ELSE NULL END)) AS 'rating',

当我在 WHERE 子句中向查询提供单个用户 ID(WHERE user_id = 1)时,这会起作用。但是,我想获取所有用户的列表会员及其评分,这就是问题发生的地方。我的查询:

SELECT DISTINCT
  U.user_id,
  (COUNT(CASE WHEN V.vote = 1 THEN 1 ELSE NULL END)-COUNT(CASE WHEN V.vote = 0 THEN 1 ELSE NULL END)) AS 'rating',
  username
  FROM users U
    LEFT JOIN tips T ON U.user_id = T.user_id
    LEFT JOIN votes V ON T.tip_id = V.tip_id

基本上,这应该找到 votes 中tip_id等于tips表中tip_id的所有行,然后找到tips<中的所有行/strong> 表,其中 user_id 等于 users 表中的 user_id。我遇到的问题是,即使 users 表中有 83 行,我也只返回一行。投票表有 287 行,其中 257 票赞成,30 票反对。我得到的这一行的评级为 227(即 257-30),这意味着投票表中的所有行都被计算在内,而不仅仅是我正在查找的特定 user_id 的行。我想获得每个用户对其所有提示的赞成票和反对票数。

基本上,对于用户表中的每个 user_id,它应该找到该用户 id 的所有tip_id,然后找到这些tip_id 的所有投票。

这是不可能的,还是我错过了一些非常明显的东西?

编辑:我应该注意,如果我完全删除 COUNT() 行,那么查询就会工作,返回正确的行数......它只是没有投票信息。

I've got a weird question. I have a user table where each user has a unique user id. Users are able to post comments (tips) which are saved to a different table, each comment row also has a unique ID and also contains the user ID of the user that posted it. Finally, other users are able to vote on the comments. Votes are stored in another table along with the user ID of the person that voted, and the comment ID of the comment being voted on (no unique ID).

I calculate a person's rating as the number of up votes minus the number of down votes, so 4 up and 2 down would result in a rating of 2, simple. I want to have a member list that is sortable by a person's rating, but I'm having a bit of an issue.

I use COUNT() and CASE to determine the number of up and down votes:

(COUNT(CASE WHEN V.vote = 1 THEN 1 ELSE NULL END)-COUNT(CASE WHEN V.vote = 0 THEN 1 ELSE NULL END)) AS 'rating',

This works when I supply a single User ID to the query, in the WHERE clause, WHERE user_id = 1. However, I want to get a list of all members and their ratings, and that's where the problem happens. My query:

SELECT DISTINCT
  U.user_id,
  (COUNT(CASE WHEN V.vote = 1 THEN 1 ELSE NULL END)-COUNT(CASE WHEN V.vote = 0 THEN 1 ELSE NULL END)) AS 'rating',
  username
  FROM users U
    LEFT JOIN tips T ON U.user_id = T.user_id
    LEFT JOIN votes V ON T.tip_id = V.tip_id

Basically, this should find all rows in the votes where the tip_id is equal to the tip_id in the tips table, then all rows in the tips table where the user_id is equal to the user_id in the users table. The problem I'm having is that I only get a single row returned, even though there are 83 rows in the users table. There are 287 rows in the votes table, with 257 up votes and 30 down votes. This single row that I get has a rating of 227 (which is 257-30), which means that ALL the rows in the votes table are being counted, not just those for the specific user_id that I'm looking for. I want to get the number of up- and down-votes that each user has had for all of his or her tips.

Basically, for every user_id in the users table, it should find all of the tip_id's for that user id, then all votes for those tip_id's.

Is this not possible, or am I missing something horribly obvious?

EDIT: I should note that if I remove the COUNT() line completely, then the query works, returns the proper number of rows...it just doesn't have the vote information.

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

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

发布评论

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

评论(1

总以为 2024-12-05 01:24:53
SELECT 
  U.user_id,
  (COUNT(CASE WHEN V.vote = 1 THEN 1 ELSE NULL END)-COUNT(CASE WHEN V.vote = 0 THEN 1 ELSE      NULL END)) AS 'rating',
 username
 FROM users U
  LEFT JOIN tips T ON U.user_id = T.user_id
  LEFT JOIN votes V ON T.tip_id = V.tip_id
 GROUP BY U.user_id, rating, username

我没有测试过。但我认为你错过了 group by 子句。

SELECT 
  U.user_id,
  (COUNT(CASE WHEN V.vote = 1 THEN 1 ELSE NULL END)-COUNT(CASE WHEN V.vote = 0 THEN 1 ELSE      NULL END)) AS 'rating',
 username
 FROM users U
  LEFT JOIN tips T ON U.user_id = T.user_id
  LEFT JOIN votes V ON T.tip_id = V.tip_id
 GROUP BY U.user_id, rating, username

I didn't test it. But i think you're missing the group by clause.

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