title: MySQL - Subtract sums from two different tables
body: <p>I would like to do join table <code>points_minus</code> and do something like <code>(SUM(p.points) - SUM(m.points)) AS points</code> instead of doing this <code>return $points - $this-&gt;getMUserPoints($user_id);</code> which is a bit much imo. And if it can be done without using sub queries that would be great.</p>
  
  <pre><code>    /**
      * Get plus user points.
      *
      * @param (int) $user_id
      */
      public function getUserPoints($user_id) {
          if($q = $this-&gt;db-&gt;mysqli-&gt;prepare("SELECT SUM(points) FROM points_plus WHERE user_id = ?"))
          {
              $q-&gt;bind_param("i", $user_id);
              $q-&gt;execute();
  
              $q-&gt;bind_result($points);
  
              $q-&gt;fetch();
              $q-&gt;close();
              return $points - $this-&gt;getMUserPoints($user_id);
          }
              return false;
      }
  
  
      /**
      * Get minus user points.
      *
      * @param (int) $user_id
      */
      public function getMUserPoints($user_id) {
          if($q = $this-&gt;db-&gt;mysqli-&gt;prepare("SELECT SUM(points) FROM points_minus WHERE user_id = ?"))
          {
              $q-&gt;bind_param("i", $user_id);
              $q-&gt;execute();
  
              $q-&gt;bind_result($points);
  
              $q-&gt;fetch();
              $q-&gt;close();
              return $points;
          }
              return false;
      }
  </code></pre>
  
  <p>Got this far ... but it's returning a negative number: </p>
  
  <pre><code>"SELECT (SUM(p.points) - SUM(m.points) )
  FROM points_plus p
  LEFT JOIN
  points_minus m 
  ON p.user_id = m.user_id
  WHERE p.user_id = ?
  GROUP BY p.user_id"))
  </code></pre>