2013-08-23 6 views
0

나는 다음과 같은 쿼리가 :다른 분야와 한 테이블에서 여러 행을 선택

SELECT count(id) from table_1 WHERE field_1 = 1

SELECT count(id) from table_1 WHERE field_2 = 1

SELECT count(id) from table_1 WHERE field_2 = 1

이가 .. 단일 쿼리에서 할 수 하나 테이블은 사용하지만같은 3 개의 출력 :

count(id) | count(id) | count(id)<br> 
    12  | 44  | 55 

답변

2

예는 다음과 유사한 CASE 표현식으로 집계 함수를 사용하여 결과를 얻을 수 있습니다 :

select 
    sum(case when field_1 = 1 then 1 else 0 end) field1Total, 
    sum(case when field_2 = 1 then 1 else 0 end) field2Total 
from table_1 

당신은 당신이 총하려는 나머지 항목에 대한 더 많은 sum(case...) 표현을 추가합니다.

+0

이것은 대단합니다 !!!! 감사합니다. 많은 블루 페어 ... 당신은 내 하루를 .. – user2324375

0
Select Distinct (Select Count(id) from table_1 where field1=1)as id1, (Select Count(id) from table_1 where field2=1)as id2 from table_1 
관련 문제