Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7898989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:35:07+00:00 2026-06-03T08:35:07+00:00

I have MySQL query which I think needs a subquery. I’d like to count

  • 0

I have MySQL query which I think needs a subquery. I’d like to count the total number of “up” votes on each of many comments and determine whether a given user has already voted on each comment:

Here are my tables:

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  

here’s the output i’m getting:

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

Here’s the desired output:

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

Here’s my query:

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  

You’ll notice these 2 outputs are identical except for voter.

What my query is missing is a way to determine whether a given user, for example with voter=acct_id=5, was the one who voted on any of the comments. Without that condition, the query picks the first voter in the list which for comment_id=5 is voter=1.

So my question is I think I need to insert the following subquery:

SELECT from votes where voter='X'

I’m just not sure where or how. Putting it in parentheses in between the from and votes above eliminates the sum() function so I’m stuck.

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:35:08+00:00Added an answer on June 3, 2026 at 8:35 am

    If I’ve understood you correctly from your comments above, I think all you need to do is (outer) join the votes table to your query another time, this time only on the votes of the account in question:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following mysql query which I am running with php like so.
I have a complicated MySQL query which takes a lot of time, selecting from
I have this query which works correctly in MySQL. More background on it here
I have a query which works fine in MySQL, but when I run it
Hi I have a query which is very straightforward to write in mysql but
Hi i have a small query which takes data from one mysql database and
I have one main array which is the returned data from a MySQL query
I need to have MySQL query like this one: UPDATE table_name SET 1 =
I have a MySQL query like this: SELECT DAYNAME(CreatedAt) AS TheDay, SUM(Amount) AS TheSum
I have a MySQL query in which I want to include a list of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.