2013-11-28 70 views
0
從查詢0值

我的查詢是忽略在MySQL

SELECT cod_material, (sum(cantidad_actual > 0))>1 AS cantidad FROM ingreso_material GROUP BY cod_material ORDER BY cod_material ASC 

我的結果是

cod_material cantidad 
10001 1 
10002 0 
10004 0 
10006 0 
10007 0 
10008 0 
10010 0 
10011 0 
10012 0 
10013 0 
322010001 0 
322030001 0 
322060001 0 
322060002 0 
343310004 1 
391290001 1 
391290002 0 
391290010 1 
395050004 0 
395090003 0 
395090004 0 
395090005 0 
395090010 0 
395090011 0 
395140001 0 
397300013 1 

我需要忽略cod_material已在cantidad 0值,我想忽略與價值觀0我該怎麼做?

回答

1

您可以通過在ORDER BY子句前面的SQL末尾插入HAVING cantidad != 0來完成此操作。

0

@ staticsan的回答是對的!但在我個人看來,下面的查詢比你更好(爲了可讀性)。您的查詢在SELECT部分​​中有比較運算符。

SELECT cod_material, sum(cantidad_actual) AS cantidad 
FROM ingreso_material 
GROUP BY cod_material 
HAVING SUM(candtidad_actual) > 0 
ORDER BY cod_material ASC; 

更正的查詢如下。這看起來更好?

SELECT cod_material, (sum(cantidad_actual > 0))>1 AS cantidad 
FROM ingreso_material 
GROUP BY cod_material 
HAVING cantida > 0 
ORDER BY cod_material ASC 

順便說一句,假定cod_material僅具有一個cantidad_actual哪個值大於0,則你的查詢不能顯示它。比方說,cantidad_actual是10.

  • (總和(cantidad_actual> 0))> 1
  • SUM(10> 0)= 1
  • (1)> 1 =假= 0