2011-09-14 36 views
0

我有兩個表在mysql數據庫中,「點」和「用戶點」。 e.g如何查找使用另一個表作爲參考信息

點:

id | pointvalue | message 
---------------------------- 
1 | 5   | comment 

Userpoints:

id | uid |pid | timestamp 
------------------------- 
89 | 5 | 1 | timestamp 

我將如何獲得在userpoints表點的總和時,它正受到userpoints的PID聯繫?

使用mysql查詢?

回答

1
SELECT up.uid, SUM(p.pointvalue) total_points 
    FROM Userpoints up 
LEFT JOIN Points p 
     ON up.pid = p.id 
    WHERE up.uid = 5; 
+0

我將如何選擇點,其中uid =變量?那麼,對於有5個或10個人的人來說,積分是多少? – Aaron

+0

@Matthew:編輯了添加WHERE條款的答案。如果你打算使用'WHERE'子句,那麼你不需要'GROUP BY'子句。 – Shef

0

select sum(p.pointvalue) from userpoints up left join points p on up.pid = p.id group by up.uid

相關問題