【问题标题】:mysql subquery issue on comments voting关于评论投票的mysql子查询问题
【发布时间】:2012-05-11 12:58:02
【问题描述】:

我有 MySQL 查询,我认为它需要一个子查询。我想计算对许多 cmets 中每一个的“赞成”投票总数,并确定给定用户是否已经对每条评论投票:

这是我的桌子:

Comments Table:
  comment_id  comment      acct_id     topic_id    comment_date
      5        hello5        2             1            9:00am
      7        hello7        3             1           10:00am

  Votes Table:
  comment_id   vote        acct_id     topic_id
     5          1             1            1
     7          1             4            1
     5          1             5            1  

这是我得到的输出:

  comment_id  commenter  comment  voter   sum    did_i_vote 
      5           2      hello5     1      2          1 
      7           3      hello7     4      1          1

这是所需的输出:

  comment_id  commenter  comment  voter   sum    did_i_vote 
      5           2      hello5   **5**    2          1 
      7           3      hello7     4      1          1

这是我的查询:

SELECT votes.acct_id as voter, comments.comment_id, comment, comments.acct_id as 
commenter, SUM(vote) as sum, vote as did_i_vote
from votes 
right join comments 
on votes.comment_id=comments.comment_id 
join accounts on comments.acct_id=accounts.acct_id 
where topic_id=1 
group by comments.comment_id order by comment_date desc  

您会注意到这 2 个输出是相同的,除了 voter

我的查询缺少的是一种确定给定用户(例如voter=acct_id=5)是否是对任何 cmets 投票的人的方法。如果没有该条件,查询将选择列表中的第一个voter,对于comment_id=5,它是voter=1

所以我的问题是我认为我需要插入以下子查询:

SELECT from votes where voter='X'

我只是不确定在哪里或如何。把它放在上面的fromvotes 之间的括号中消除了sum() 函数,所以我被卡住了。

有什么想法吗?

【问题讨论】:

  • 但是,如您的示例所示,如果某条评论收到了来自多个用户的投票……您的输出中应该出现哪个投票者?
  • 嗨,eggyal,我希望查询不仅可以选择所有 cmets 及其总票数,还可以选择 acct_id=X 是否对评论进行了投票。
  • 这不是did_i_vote 列的作用吗?哪个选民应该出现在voter 列中?
  • 是的,这就是 did_i_vote 列应该要做的事情。 voter="X" 投票是否应该出现在该列中

标签: mysql join count subquery


【解决方案1】:

如果我从上面的 cmets 中正确理解了您,我认为您需要做的就是(外部)再次将 votes 表加入到您的查询中,这次仅针对相关帐户的投票:

SELECT
   comments.comment_id       AS comment_id,
   comments.acct_id          AS commenter,
   comment                   AS comment,
-- votes.acct_id             AS voter,                        -- ambiguous
   SUM(votes.vote)           AS sum,
   my_votes.vote IS NOT NULL AS did_i_vote
FROM
             votes
  RIGHT JOIN comments ON votes.comment_id=comments.comment_id
        JOIN accounts ON comments.acct_id=accounts.acct_id    -- what purpose ?
  LEFT  JOIN votes AS my_votes ON
               my_votes.commentid=comments.comment_id
           AND my_votes.acct_id=@my_acct_id
WHERE topic_id = 1                                            -- ambiguous
GROUP BY comments.comment_id
ORDER BY comment_date DESC

【讨论】:

  • 嗨eggyal,太棒了!这完全符合我的需要。你能解释一下外连接与子查询的区别吗?我想我仍然不确定何时使用一个与另一个。
  • @timpeterson:外连接将两个表连接在一起,但即使连接条件没有匹配项,它也不会消除 LEFTRIGHT 表中的任何记录(如指定):这些记录仍然出现,但在连接另一侧的表的每一列中都有NULL。子查询是在您的查询中对另一个 SELECT 查询的任何使用 - 它不需要连接到您的其他表,但如果它像任何其他连接一样,它可以是内部或外部;例如,您可以在 WHERE 子句中使用子查询,而不是将它们与查询中的表连接起来。这有帮助吗?
猜你喜欢
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 2011-04-21
  • 1970-01-01
  • 2013-05-13
  • 2014-03-15
相关资源
最近更新 更多