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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:04:47+00:00 2026-05-30T07:04:47+00:00

Yesterday I have been testing one single query for over hours and hours, but

  • 0

Yesterday I have been testing one single query for over hours and hours, but I never succeeded. These are the three tables:

USERS:
#### id: 1 ##### name: Admin ##### Hometown: The Hague

POSTS:
#### id: 1 ##### userid: 1 ##### title: Test I ##### opinion: agree
#### id: 2 ##### userid: 1 ##### title: Nope.. ##### opinion: disagree

REACTIONS:
#### id: 1 ##### userid: 1 ##### opinion: agree
#### id: 2 ##### userid: 1 ##### opinion: disagree

And this is what I want:
I want the basic information of the user (name, hometown, etc) and I want to count how much compliments (post – opinion: agree), how much complaints (post – opinion: disagree), how much positive reactions (reaction – opinion: agree) and how much negative reactions (reaction – opinion: disagree) this person has posted.

This is the query I use now:

    SELECT
        u.name, u.hometown,

        SUM(IF(r.opinion="disagree",1,0)) AS agrees
        SUM(IF(r.opinion="disagree",1,0)) AS disagrees,

        SUM(IF(p.opinion="agree",1,0)) AS compliments,
        SUM(IF(p.opinion="disagree",1,0)) AS complaints

    FROM
        users AS u

    LEFT JOIN
        reactions AS r
    ON
        r.userid = u.id

    LEFT JOIN
        posts AS p
    ON
        p.userid = u.id

    WHERE
        u.id = 1

The problem is that this does not give me the correct information. It returns values like 8 positive reactions, though there are only two reactions in the DB.

I think it has something to do with GROUP BY p.id, r.id but I tried that and it did not work… Could someone enlighten me?

Thanks in advance!

  • 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-05-30T07:04:48+00:00Added an answer on May 30, 2026 at 7:04 am

    Depending on your DBMS, you might be able to do this:

    SELECT
        *,
        (SELECT COUNT(*) FROM POSTS WHERE POSTS.userid = USERS.id and opinion = 'agree') compliments,
        (SELECT COUNT(*) FROM POSTS WHERE POSTS.userid = USERS.id and opinion = 'disagree') complaints,
        (SELECT COUNT(*) FROM REACTIONS WHERE REACTIONS.userid = USERS.id and opinion = 'agree') positive_reactions,
        (SELECT COUNT(*) FROM REACTIONS WHERE REACTIONS.userid = USERS.id and opinion = 'disagree') negative_reactions
    FROM USERS
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This query is related to this one I asked yesterday. I have a radio
I have been trying since yesterday to get google like paging, but each time
I have a question that may have been answered over 9000 times before but
I have been an avid fan of lazy loading but yesterday I was talking
i asked this question yesterday but it doesnt seem to have asked properly so
I have been working on Hibernate since sometime and I never really paid attention
i have been using System.Timer to run a windows service but have come across
I have been searching for a solution for this issue for multiple hours today
I have problem I have never met before. I was clearing yesterday clients db
This is the continuation of the my earlier post Since yesterday I have been

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.