title: Sum function in mysql with inner query
body: <p>I have a query like following:</p>
  
  <pre><code>    select b.*, SUM(h.points) as points
      from brands b left join
      histories h
      on b.id = h.brandid and h.userId = $userId
      where b.id=$brandId and b.active=1 
      group by b.id
      limit 1
  </code></pre>
  
  <p>I want to sum only points for following cases:</p>
  
  <pre><code>where h.type != 10, h.points !=0 and points should be between two dates like this: h.time between '2016-06-01' and '2016-06-31'...
  </code></pre>
  
  <p>I'm thinking this could be done by using a sub-query but I'm not sure how... Can you help me out guys???</p>
  
  <p>The desired output would be:</p>
  
  <pre><code>userId  brandId  points 
  
  1         64      155
  
  1         15      100
  </code></pre>
  
  <p>And so on that I have each summed points nicely for each user that has scored those points in between two dates and that the type of points is not != 10;</p>
  
  <p>The table structure is like following:</p>
  
  <pre><code>Brands =&gt; Histories &lt;= Users
  </code></pre>