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
Simply join the queries :
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.
You will get null values where there are no given or gotten votes.