我有几张桌子:
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)
问题是,id1 带来了 81 分(因为他已经被激活了 3 次......)
这是我当前的 mysql 字符串(缩短...)
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
..到目前为止,它“几乎”可以按需要工作。
但是:用户可能有几个合作伙伴将他“激活”到“激活”表中。
现在,当调用它时,我得到了 points_total 的重复(它将被计算多次,因为 uesr 已被合作伙伴激活......)这在排名列表中没有多大意义......
我怎样才能为每个 user_id 只获得“一个”points_total
希望你明白,我想要实现什么......非常感谢!