title: MySQL query only returns value if rows exist
body: <p>I have the following query which works perfectly, but only if each select finds a row.</p>
  
  <p>I've attempted to add IFNULL to return 0 if no rows were found but I'm still not getting the correct return.</p>
  
  <pre><code>SELECT IFNULL(paid_value,0)-IFNULL(ordered_value,0)+IFNULL(credit_value,0) AS account_balance
              FROM
              (
                SELECT customer_id, SUM(order_total) AS ordered_value
                FROM orders
                WHERE customer_id = '1'
                GROUP BY customer_id
              ) AS orders
              LEFT JOIN
              (
                SELECT customer_id, SUM(amount) AS paid_value
                FROM transactions
                WHERE customer_id = '1'
                GROUP BY customer_id
              ) as payments
              ON orders.customer_id = payments.customer_id
              LEFT JOIN
              (
                  SELECT customer_id, SUM(amount) AS credit_value
                  FROM credits
                  WHERE customer_id = '1'
                  GROUP BY customer_id
              ) as credits
              ON orders.customer_id = credits.customer_id
  </code></pre>
  
  <p>This query currently returns empty, it's not returning NULL or 0.</p>
  
  <p>When I run </p>
  
  <pre><code>              SELECT customer_id, SUM(order_total) AS ordered_value
                FROM orders
                WHERE customer_id = '1'
                GROUP BY customer_id
  </code></pre>
  
  <p>It also returns empty, not NULL or 0, unless there's a row. In order for the full query to work, each of the 3 separate queries need to have a row in them.</p>
  
  <p>Any ideas?</p>