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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:10:01+00:00 2026-06-07T08:10:01+00:00

I have the following database. One user can write one or more posts. Votes

  • 0

I have the following database. One user can write one or more posts. Votes is many-to-many.

A user writes a post. Anyone can vote for the post, and thus the vote is saved in the vote table.

I need to display all the posts on a page. On each post, the post’s author, post_votes, and the votes of the author need to be displayed.

My tables are:

Table: user

id | user_name
1  | John
2  | Sam
3  | Susan

Table: post

id | post_title | user_id
1  | Hello      | 1
2  | Sunday     | 1
3  | Monday     | 2
4  | Sun        | 1
5  | Sun        | 3


Table: votes

id | post_id

1  |  1
2  |  1
3  |  3
4  |  2
5  |  3
6  |  1

I need the query to return the following resultset:

post_id | post_votes | user_id | user_votes
1       | 3          | 1       | 4 
2       | 1          | 1       | 4
3       | 2          | 2       | 2  
4       | NULL       | 1       | 4    
5       | NULL       | 3       | NULL

I tried all sorts of combinations, but my brain just won’t work. Also, would be very thankful if there is a better way to design the database so that retrieving the above can be made easy.

  • 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-07T08:10:04+00:00Added an answer on June 7, 2026 at 8:10 am

    This is a little complicated. In cases like this, you often want to calculate each aggregated column separately. In this case, use one subquery to get the number of posts and another to get the number of users.

    select p.post_id, pv.post_votes, uv.user_id, uv.user_votes
    from post p left outer join
         (select v.post_id, count(*) as post_votes
          from votes v
          group by v.post_id
         ) pv
         on p.id = pv.id left outer join
         (select p.user_id, count(*) as user_votes
          from votes v join
               post p
               on p.id = v.post_id
          group by p.user_id
         ) uv
         on p.user_id = uv.user_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have database with the following associations: One Client to Many Intakes One
I have the following code, and database. My intent is the end user can
I have the following column specified in a database: decimal(5,2) How does one interpret
I have the following scenario: Entities are loaded from the database. One of them
Currently I have a database with the following relationships: One Client has multiple Entities
User can have the following access rights: Read = 1 Create = 2 Edit
Just another one regarding many to many relationship in JPA. I have the following
I have some trouble with this. I have one database with the following tables:
I have the following database table object: public class Goal { @DatabaseField(generatedId = true)
I have the following database structure. ID | Name | Marks I am trying

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.