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 8195271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:04:52+00:00 2026-06-07T05:04:52+00:00

I wonder how the following could be solved with a mysql query, but don’t

  • 0

I wonder how the following could be solved with a mysql query, but don’t really know how to proceed

i have 3 tables (i will try to keep them simple)

          post

       id  | content 
           |
       1   | bla, bla..

          comment                          |                votes
                                           |
id | post_id | comment | date              |        id | comment_id | date 
                                           |           |            |
5  |    1    |  derp   | 2012-07-06        |        1  |     5      | 2012-07-07
6  |    1    |  nherp  | 2012-07-07        |        2  |     5      | 2012-07-08
7  |    1    |  yada   | 2012-07-08        |        3  |     5      | 2012-07-09
8  |    1    |  lala   | 2012-07-09        |        4  |     6      | 2012-07-10
                                           |        5  |     6      | 2012-07-11    
                                           |        6  |     7      | 2012-07-12
                                           |        7  |     8      | 2012-07-13
                                           |

they are tied to each other in this way

comment.post_id is a fk to post.id

votes.comment_id is a fk to comment.id

What i’m trying to discover is how many votes have been given to the entire post after each comment. Or in other words, how many votes have been given to entire post after the submission of each comment.
So, for this example case i presented, the result i was expecting would be the following table

                                      result

                  id | post_id | comment | date        | votes_after_submission
                     |         |         |             | 
                  5  |    1    |  derp   | 2012-07-06  |          7
                  6  |    1    |  nherp  | 2012-07-07  |          6
                  7  |    1    |  yada   | 2012-07-08  |          5
                  8  |    1    |  lala   | 2012-07-09  |          4

This is giving my brain a knot, i tried using the CASE keyword in the COUNT statement, but couldn´t actually make it work for this problem, can anyone give me some clues, or point me in the right direction or maybe just throw me some google keywords to search for as i didn´t find anything on this problem.

  • 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-07T05:04:55+00:00Added an answer on June 7, 2026 at 5:04 am

    If I understand your question and example result correctly, try this:

    SELECT C.id,C.post_id,C.comment,C.date, 
           (SELECT COUNT(*)
            FROM votes AS V 
            WHERE V.date > C.date 
              AND V.comment_id IN (SELECT id FROM comment AS C2 
                                   WHERE C2.post_id=C.post_id)) AS votes_after_submission
    FROM comment AS C
    ORDER BY C.post_id, C.date
    

    I’m not sure it’s the most efficient, but it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder how the following could be solved with a mysql query, but don't
The following script works for me, but I wonder if it could be done
Wonder how I could do the following wit jquery - I have a url
wonder whether someone can help me with the following one... I have a struct
I have the following program. I wonder why it outputs -4 on the following
i wonder how i can solve the following problem. i have a horizontal scrollbar
This is a total newbie question, but I wonder if someone could assist with
I wonder if the following is possible: I have a dll that I have
Hello there fellow Stackers! I wonder if anybody could tell me what the following
I wonder if it is possible to have the following data structure in a

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.