Mysql LEFT JOIN and SUM and show 0 instead of NULL

This will do what you are asking. It will replace the null values with 0 if there is an order with no detail record.

SELECT o.order_id, 
       COALESCE(sum(d.qty), 0)
FROM orders o
LEFT JOIN details d ON o.order_id = d.order_id
GROUP BY o.order_id;

Link to SQL Fiddle Example.


Try this:

SELECT o.order_id, IFNULL(SUM(d.qty),0) AS total
FROM orders o
    LEFT JOIN details d ON o.order_id = d.order_id
 GROUP BY order_id