2011-09-14 2 views
0

mysql 데이터베이스에 "points"와 "userpoints"라는 두 개의 테이블이 있습니다. 예컨대다른 테이블을 참조로 사용하여 정보를 찾는 방법

포인트 :

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

Userpoints :이 userpoints의 pid로 연결 될 때

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

가 어떻게이 userpoints 테이블에있는 점의 합계를받을 수 있습니까?

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 = 변수를 선택할 것인가? 그렇다면 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

관련 문제