Forum Moderators: coopster

Message Too Old, No Replies

Mysql group by Right Join

Incorrect Results

         

Tommybs

11:14 am on Jan 27, 2011 (gmt 0)

10+ Year Member



I have the following 2 tables

+--------+--------+-----+
| pic_id | rating | uid |
+--------+--------+-----+
| 1 | 4 | 1 |
| 2 | 3 | 1 |
| 3 | 6 | 2 |
| 4 | 2 | 1 |
| 5 | 1 | 2 |
+--------+--------+-----+

+------------+--------+-----+
| comment_id | rating | uid |
+------------+--------+-----+
| 1 | 1 | 2 |
| 2 | 3 | 1 |
| 3 | 4 | 3 |
| 4 | 3 | 2 |
+------------+--------+-----+

What I want to do is get an overall score for a user based on all their ratins (so assume comment 1 belongs to uid 2 as well as pic3) The object belong to the user and the rating is a sum already calculated by a trigger.
What I want to do is get an overall score for the user based on how people have rated all their pics or comments. I've tried like so

:


select
sum(p.rating) as prating,
sum(c.rating) as crating ,
(sum(c.rating) + sum(p.rating)) as orating ,
p.uid as puid,
c.uid as cuid
from pic_rating as p
right join comment_rating as c
on p.uid = c.uid
group by p.uid, c.uid
order by orating desc


But this produces the following, incorrect results:

prating 14 crating 8 uid is 2 and orating is 22
--expected prating 7, crating 4, orating 11
prating 9 crating 9 uid is 1 and orating is 18
--expected prating 9, crating 3 orating 12
prating crating 4 uid is 3 and orating is 4
--expected prating 0, crating 4, orating 4

Can anybody see what I am doing wrong here? Or suggest an alternative way to do this? I don't really want to run a second query if I can avoid it, but if that's my only option and then performing the addition in php I guess that's what I'll have to do

Cheers

Tommybs

6:24 pm on Jan 31, 2011 (gmt 0)

10+ Year Member



Anybody able to help with this? Or does this need moving to another forum?

Matthew1980

7:36 pm on Jan 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there TommyBs,

Well theres no harm in posting this question in the sql forum :) and technically it is a sql question.

Good luck.

Cheers,
MRb

Tommybs

7:52 pm on Jan 31, 2011 (gmt 0)

10+ Year Member



Yeah that's what I thought. I always forget it's there as I'm so used to seeing other peoples sql questions in this forum!

Matthew1980

8:04 pm on Jan 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>Yeah that's what I thought. I always forget it's there as I'm so used to seeing other peoples sql questions in this forum!

There does seem to be a fine line between the two especially as the two languages interact so seamlessly.

Cheers,
MRb

Tommybs

5:04 pm on Feb 1, 2011 (gmt 0)

10+ Year Member



Well I worked out how to do this using the following query:

SELECT p.prating,c.crating, p.uid as puid, c.uid as cuid, (p.prating + c.crating) as orating FROM (SELECT sum(rating) as prating, uid from pic_rating group by uid)as p right join (SELECT sum(rating) as crating,uid FROM comment_rating group by uid) as c
on p.uid = c.uid

Then all I need is quick check in the code to just use one of the ids i.e if p.uid is null but there is a c.uid row