Get SUM in related table. If not found get 0 as sum
<p>I have this query:</p>

<pre><code>$sql = "SELECT m.id FROM members m
          LEFT JOIN orders o ON o.user_id = m.id AND
            YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND
            MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
          LEFT JOIN transactions t ON t.user_id = o.user_id";
</code></pre>

<p>I need to get sum of transactions.points, so I do this:</p>

<pre><code>$sql = "SELECT m.id, COALESCE(SUM(t.points), 0) AS total_points FROM members m
          LEFT JOIN orders o ON o.user_id = m.id AND
            YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND
            MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
          LEFT JOIN transactions t ON t.user_id = o.user_id";
</code></pre>

<p>But when i execute this, I will get only rows with some transactions (and orders). What I must to do for getting 0 as total_points when no transactions found?</p>

<p>Text description: I want to get sum of points reward (from transactions table) of orders (orders table), where order was created in last month.</p>

<p>Thanks!</p>