如何使用另一个表作为参考来查找信息

时间:2011-09-14 10:16:22

标签: mysql

我在mysql数据库中有两个表,“points”和“userpoints”。 e.g

点数:

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

Userpoints:

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

如果用户点中的pid链接到用户点表中,我将如何获得这些点的总和?

使用mysql查询?

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

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