 <p>For example if brand id is <code>100</code>:</p> <pre><code>select b.id, sum(h.points),h.brandId from brands b left join histories h on b.id = h.brandid and h.userId = 2866 where b.id=100 group by b.id limit 1; </code></pre>