I have several tables:
table user:
id firstname lastname
1 John Doe
2 Jane Skith
3 will Smith
...
table member:
member_id member_user_id member_status member_activated_by
10 1 yes partner 1
11 1 yes partner 2
12 1 yes partner 3
13 2 yes partner 2
14 3 no ----
...
table points:
points_id points_user_id points_value points_date
10 1 10 2012-02-15
11 2 15 2012-02-15
12 2 20 2012-02-15
13 1 5 2012-02-15
14 3 30 2012-02-15
15 1 12 2012-02-15
...
So results SHOULD be:
id1 - John Doe: 27 Points
id2 - Jane Skith: 35 Points
id3 - will Smith: nothing (not activated)
Problem is, that id1 brings 81 Points (as he has been actiavted 3 times …)
This is my current mysql string (shortened…)
SELECT points_user_id, SUM( points_value ) AS points_total, id, firstname, lastname
FROM user
JOIN member ON member.member_user_id = user.id
JOIN points ON points.points_user_id = user.id
WHERE 1
AND member_status = 'yes'
GROUP BY points_user_id
ORDER BY points_total DESC
LIMIT 0 , 100
.. so far it “almost” works as desired.
BUT: A user may have sevaral partners that activated him “into the activation” table.
Now when this is called, I get a duplication of points_total (it will be counted as many times, as the uesr has been activated by partners … ) This does not make to much sense in a rankling list…
How can I get only “one” points_total for each user_id
Hope you understand, what I want to achieve … Thanks a lot!
I think that this should work, instead of join with activation make a subselect where you will get only one row per user_id then join with this subselect, I’m not sure if this is the MySql sintax but it should show you the way. The only remark is that the fields that you choose inside the inner query may not change for a same user_id