쿼리문 질문드립니다. 채택완료
Copy
$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개
채택된 답변
+20 포인트
3년 전
이렇게 해보세요
Copy
$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";
로그인 후 평가할 수 있습니다
답변에 대한 댓글 1개
댓글을 작성하려면 로그인이 필요합니다.
3년 전
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]}'
이렇게 할 수도 있습니다.
로그인 후 평가할 수 있습니다
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인