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.
If I understand your question and example result correctly, try this:
I’m not sure it’s the most efficient, but it works.