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

  • Home
  • SEARCH
  • 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 6008133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:46:53+00:00 2026-05-23T01:46:53+00:00

I have table with votes where data is written in this format: id_user|id_user2|value 1|2|-1

  • 0

I have table with votes where data is written in this format:

id_user|id_user2|value
1|2|-1
1|3|1
5|3|1
2|1|-1

id_user is user who voted, id_user2 is user who got vote and value represents if he got positive or negative vote. Well and my problem is that i would like to show how stands each user in giving and getting votes in same output. Below is how i would like output from above input would look:

id_user|given|gotten
1|0|-1
2|-1|-1
3|0|2
5|1|0

I don’t know how to do this, i just know how i can show this with two separate queries. In this case it would be:

SELECT id_user2,SUM(value) FROM `table` GROUP BY id_user2

and

SELECT id_user,SUM(value) FROM `table` GROUP BY id_user
  • 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-23T01:46:55+00:00Added an answer on May 23, 2026 at 1:46 am

    Simply join the queries :

    SELECT
      gotten.id_user,
      gotten.gotten,
      given.given
    FROM
    (
      SELECT
        id_user2   AS id_user,
        SUM(value) AS gotten
      FROM
        table
      GROUP BY
        id_user2
    ) gotten
    JOIN -- Join the current row with rows verifying...
    (
      SELECT
        id_user,
        SUM(value) AS given
      FROM
        table
      GROUP BY
        id_user
    ) given ON given.id_user =  gotten.id_user -- ... this is the same user.
    

    N.B. if the user did not give or did not get votes, it won’t return anything for him. If you need a result for these cases, use a full outer join to join even if there are not rows matching your condition in the left or right table.

    SELECT
      gotten.id_user,
      gotten.gotten,
      given.given
    FROM
    (
      SELECT
        id_user2   AS id_user,
        SUM(value) AS gotten
      FROM
        table
      GROUP BY
        id_user2
    ) gotten
    FULL OUTER JOIN -- Join the current row with rows verifying...
    (
      SELECT
        id_user,
        SUM(value) AS given
      FROM
        table
      GROUP BY
        id_user
    ) given ON given.id_user =  gotten.id_user -- ... this is the same user.
    

    You will get null values where there are no given or gotten votes.

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

Sidebar

Related Questions

So I have this query that pulls from my links and votes table and
I'm trying to figure out a SQLquery. I have a table of vote data
I have a table with a number of votes recorded in it, each vote
I have table rows of data in html being filled from a CGI application.
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I have a table with more than a millon rows. This table is used
I have a table which is full of arbitrarily formatted phone numbers, like this
I have two rating/votes tables. One for user votes and stats and another for
I have this php algorithm ranking function(hacker news): function calculate_score($votes, $item_hour_age, $gravity=1.8){ return ($votes
I have a table with the following columns: user_id, key, value, and flag. The

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.