쿼리문 질문드립니다.

쿼리문 질문드립니다.

QA

쿼리문 질문드립니다.

답변 2

본문


$year_start =  date('Y-01-01');
$month_start =  date('Y-m-01');
$month_end =  date('Y-m-t');
$sql_month = "SELECT sum(wr_45) as tot_month FROM {$write_table} where wr_29 between '{$month_start }' and '{$month_end }' and mb_id = '{$member[mb_id]}' ";

 

이번달에 wr_45의 총 금액을 구하는 쿼리문입니다.

쿼리문에 이번달과 이번년도의 값을 각각 뽑을수도 있나요?

 

 

쿼리문을 두번 돌리는건 비효율적일거 같아서... 혹시나 질문드립니다.

 

 

이 질문에 댓글 쓰기 :

답변 2

이렇게 해보세요


$year_start =  date('Y-01-01');
$month_start =  date('Y-m-01');
$month_end =  date('Y-m-t');
$sql_month = "
SELECT  a.tot_month, b.tot_year from ( SELECT sum(wr_45) as tot_month FROM {$write_table} where wr_29 between '{$month_start }' and '{$month_end }' and mb_id = '{$member[mb_id]}' ) a, (SELECT sum(wr_45) as tot_year FROM {$write_table} where wr_29 between '{$year_start }' and '{$month_end }' and mb_id = '{$member[mb_id]}' ) b";

 SELECT sum(wr_45) as tot FROM {$write_table} where wr_29 between '{$month_start }' and '{$month_end }' and mb_id = '{$member[mb_id]}' 

union

SELECT sum(wr_45) as tot FROM {$write_table} where wr_29 between '{$year_start }' and '{$month_end }' and mb_id = '{$member[mb_id]}'
 

이렇게 할 수도 있습니다.

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 59,586
© SIRSOFT
현재 페이지 제일 처음으로