2013-08-22 131 views
0

我有這個疑問MySQL查詢顯示,而不是NULL - 0

SELECT 
A.cod_material, 
SUM(B.cantidad_pedido) as cantidad_pedido 
FROM materiales AS A LEFT JOIN ingreso_material AS B 
on A.cod_material = B.cod_material GROUP BY A.cod_material; 

我的結果是

cod_material cantidad_pedido 
-------------------------------- 
10001    NULL 
10002    25125 
10004    1283 
10006    NULL 

而且我想讓它顯示的,而不是NULL(0)

321010001    0 

Ty for your help

+0

最好的辦法是改變你的表格,並給他們的0 – Sebastien

回答

1

使用COALESCE()這裏。該函數返回的第一個NULL的值。

SELECT 
    A.cod_material, 
    COALESCE(SUM(B.cantidad_pedido), 0) as cantidad_pedido 
FROM materiales AS A LEFT JOIN ingreso_material AS B 
    on A.cod_material = B.cod_material GROUP BY A.cod_material; 
1

使用一個case

SUM(case when B.cantidad_pedido IS NULL 
     then 0 
     else B.cantidad_pedido 
    end) as cantidad_pedido 

isnull

SUM(ISNULL(B.cantidad_pedido, 0)) as cantidad_pedido 
+0

哇默認值謝謝,這是非常快:)和它的工作原理 –