2013-11-28 3 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 0 값이 있는지를 무시해야 할

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 

입니다입니다 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 

BTW, cod_material을 가정하는 것은 하나의 cantidad_actual 0보다 큰 값, 다음 쿼리를 표시 할 수 있습니다. 이제 cantidad_actual가 생각한 10

  • (SUM (cantidad_actual> 0))> 1
  • SUM (10> 0) = 1
  • (1)> 1 = 거짓 = 0