title: MYSQL - triple Inner Join and SUM
body: <p><strong>Here my tables:</strong><br>
  invites  </p>
  
  <pre><code>id | inviteFromID | inviteToID | timestamp | joined
  ---------------------------------------------------
  1  | 1            | 234567890  | 2013-02-02| 1
  2  | 1            | 345678901  | 2013-02-02| 1
  </code></pre>
  
  <p>player:  </p>
  
  <pre><code>id | FacebookID | first_name | last_name
  ---------------------------------------------------
  1  | 123456789  | Dude       | Master
  2  | 234567890  | Super      | Man
  3  | 345678901  | Bat        | Man  
  </code></pre>
  
  <p>Rounds:  </p>
  
  <pre><code>id | PlayerID | round | played | status
  ---------------------------------------------------
  1  | 1        | 1      | 1     | 1
  2  | 1        | 2      | 1     | 0
  3  | 2        | 1      | 1     | 1
  </code></pre>
  
  <p><strong>In this case:</strong><br>
  Player with the ID = 1 would have 1 own Point and ID = 2 would have 1 own Point.<br>
  But ID 1 invited 2 so ID = 1 in total has two points (explanation below) .</p>
  
  <p><strong>Relationships:</strong><br>
  - player.id = invites.inviteFromID<br>
  - player.id = rounds.playerID</p>
  
  <p><strong>Description:</strong><br>
  A user from the table "player" can invite ppl to a game. The games are played in rounds.<br>
  When a User played a game, the "rounds" table updated with "played = 1" and the userId and "status = 1(if won) or status = 0 (if not won)".  </p>
  
  <p>To get the points for each User isn't the problem.  </p>
  
  <p><strong>Problem:</strong><br>
  ..the problem is, that every invited user gives the inviter his points too.  </p>
  
  <p>So if user A (won 2 rounds = 2 points) invited B, and B played 3 rounds and won 3 rounds ( = 3 points), the overall points of user A are 5 ( Points from A + Points from B).<br>
  And that is true for all users A invited to the game, so if user A invited B, C, and D, he will get their points too.  </p>
  
  <p>And here I stuck. I tried to <strong>Inner Join</strong> player, invites and rounds but It didn't work and I 'm stuck right now.</p>
  
  <p><strong>Best Output would be</strong></p>
  
  <pre><code>PlayerID | ownPoints | otherPoints | totalPoints
  ---------------------------------------------------
  1        | 1         | 1           | 2
  2        | 1         | 0           | 1
  </code></pre>
  
  <p>Can someone help me please? :)  </p>
  
  <p>EDIT:<br>
  <strong>Function - getPointsFromPlayer (DEPRECATED: "points" isn't available anymore)</strong>  </p>
  
  <pre><code>SELECT 
      p.points AS OwnPoints, count(p.id) AS UsersCount, SUM(pi.points) AS SumPoints 
  FROM 
      player AS p 
  INNER JOIN invites AS i ON p.id = i.inviteFromID 
  INNER JOIN player AS pi ON i.inviteToId  = pi.facebookID
  WHERE 
      p.id = :inviter AND i.joined = 1 
  GROUP BY 
      p.id
  </code></pre>