home |
[3002]
제목 쿼리한번으로 다양한 count값 가져오기
등록일 2004.12.06 HIT 4271 작성자 폰돌
union 사용하다가 실패를 했는데 아래 방법이 되더군요..
한테이블에서 년령대별/성별 가입자수를 가져오는 query 문입니다.

$sqlstr = "select (select count(uid) from wiz_statistic where age <= 19 and gender = 1) as man10,
 (select count(uid) from wiz_statistic where age <= 19 and gender = 2) as woman10,
 (select count(uid) from wiz_statistic where age between 20 and 29 and gender = 1) as man20,
 (select count(uid) from wiz_statistic where age between 20 and 29 and gender = 2) as woman20,
 (select count(uid) from wiz_statistic where age between 30 and 39 and gender = 1) as man30,
 (select count(uid) from wiz_statistic where age between 30 and 39 and gender = 2) as woman30,
 (select count(uid) from wiz_statistic where age between 40 and 49 and gender = 1) as man40,
 (select count(uid) from wiz_statistic where age between 40 and 49 and gender = 2) as woman40,
 (select count(uid) from wiz_statistic where age >= 50 and gender = 1) as man50,
 (select count(uid) from wiz_statistic where age >= 50 and gender = 2) as woman50
";

//$sqlstr = "select * from (select count(*) as man20 from wiz_statistic where age between 20 and 29 and gender = 1) union all (select count(*)  as woman20 from wiz_statistic where age between 20 and 29 and gender = 2)";
//$sqlstr = "select (select count(*) from wiz_statistic where age between 20 and 29 and gender = 1) as man20, (select count(*) from wiz_statistic where age between 20 and 29 and gender = 2) as woman20";
$sqlqry = pg_exec($sqlstr) or die(pg_result_error());
$list = pg_fetch_array($sqlqry) or die(pg_result_error());
댓글보기 엮인글보기
헐.. case 구문 사용하면 될것을 --; 훔... 2009.06.04 X
  • 글쓴이
  • 비밀번호
  • 등록
  • [ 이전글 ]
  • [ 다음글 ]
    다음글이 없습니다.