现在我有 3 个表,第一个用于用户,第二个用于用户组,第三个用于积分(还有其他的,如果你想要一个,我可以为你创建它:P)。
表用户:
user_id
|
fname
|
active
表用户组:
id
|
group_id
|
user_id
|
active
表 user_points:
id
|
point_type
|
point_units
|
active
组中的每个用户都可以为不同的活动赚取积分。所以,我想显示用户在不同活动中获得的积分,最后是总计。
这是我到目前为止的查询:
SELECT
u.user_id AS `ID`,
u.fname AS `Name`,
SUM(case when up.point_type = 'Referrals' then up.point_units else 0 end) AS `Referrals`,
SUM(case when up.point_type = 'Email' then up.point_units else 0 end) AS `Email`,
SUM(case when up.point_type = 'Facebook' then up.point_units else 0 end) AS `Facebook`,
SUM(case when up.point_type = 'Twitter' then up.point_units else 0 end) AS `Twitter`,
SUM(case when up.point_type = 'Extra' then up.point_units else 0 end) AS `Extra`,
SUM(case when up.point_type = 'Discount' then up.point_units else 0 end) AS `Discount`,
SUM(case when u.user_id = up.user_id then up.point_units else 0 end) AS `Total`
FROM users AS u, user_groups AS uc, user_points AS up
WHERE u.user_id = uc.user_id
AND u.user_id = up.user_id
AND u.active = 1
AND uc.active = 1
AND up.active = 1
AND uc.group_id = up.group_id
AND uc.group_id = 65
ORDER BY u.fname;
我遇到的问题是查询只显示一个用户和每列中所有用户的总和。
我知道这不是正确的方法,但我不知道如何将所有内容放入查询中。此外,我将此查询提供给 PHPExcel,因此它可以生成一个很好的友好报告。
有任何想法吗?:-)