1
現在我有3個表格,第一個用於用戶,第二個用於用戶組,第三個用於點(和其他的,如果你想要一個我可以爲你創造它:P)。Mysql:取決於不同活動的表格的值的總和
表用戶: user_id | fname | active
表user_groups: 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,因此它可以生成一個很好的友好報告。
任何想法? :-)
曾爲完美,謝謝! – nomeacuerdo