忽略mysql中查询的0个值

时间:2013-11-28 03:01:36

标签: 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
321010001   1
321010002   0
321010004   0
321010006   0
321010007   0
321010008   0
321010010   0
321010011   0
321010012   0
321010013   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

我需要忽略在cantidad中具有0值的cod_material 我想忽略0的值我该怎么做?

2 个答案:

答案 0 :(得分:2)

您可以通过在HAVING cantidad != 0子句之前的SQL末尾附近插入ORDER BY来完成此操作。

答案 1 :(得分: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

BTW,假设cod_material只有一个cantidad_actual,其值大于0,那么您的查询无法显示它。假设cantidad_actual是10。

  • (sum(cantidad_actual> 0))> 1
  • SUM(10> 0)= 1
  • (1)> 1 =假= 0