쿼리문 질문드립니다.

쿼리문 질문드립니다.

QA

쿼리문 질문드립니다.

본문


$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]}'
 

이렇게 할 수도 있습니다.

답변을 작성하시기 전에 로그인 해주세요.
전체 247
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT