주문현황 월별 출력 채택완료

그래프와 결제 수단 주문현황을 월별로 출력을 하려고 합니다
코드를 보니
Copy
<?php
// 일자별 주문 합계 금액
function get_order_month_sum($date)
{
global $g5;
$sql = " select sum(od_cart_price + od_send_cost + od_send_cost2) as orderprice,
sum(od_cancel_price) as cancelprice
from {$g5['g5_shop_order_table']}
where SUBSTRING(od_time, 1, 10) = '$date' ";
$row = sql_fetch($sql);
$info = array();
$info['order'] = (int)$row['orderprice'];
$info['cancel'] = (int)$row['cancelprice'];
return $info;
}
// 일자별 결제수단 주문 합계 금액
function get_order_settle_sum($date)
{
global $g5, $default;
$case = array('신용카드', '계좌이체', '가상계좌', '무통장', '휴대폰');
$info = array();
// 결제수단별 합계
foreach($case as $val)
{
$sql = " select sum(od_cart_price + od_send_cost + od_send_cost2 - od_receipt_point - od_cart_coupon - od_coupon - od_send_coupon) as price,
count(*) as cnt
from {$g5['g5_shop_order_table']}
where SUBSTRING(od_time, 1, 10) = '$date'
and od_settle_case = '$val' ";
$row = sql_fetch($sql);
$info[$val]['price'] = (int)$row['price'];
$info[$val]['count'] = (int)$row['cnt'];
}
// 포인트 합계
$sql = " select sum(od_receipt_point) as price,
count(*) as cnt
from {$g5['g5_shop_order_table']}
where SUBSTRING(od_time, 1, 10) = '$date'
and od_receipt_point > 0 ";
$row = sql_fetch($sql);
$info['포인트']['price'] = (int)$row['price'];
$info['포인트']['count'] = (int)$row['cnt'];
// 쿠폰 합계
$sql = " select sum(od_cart_coupon + od_coupon + od_send_coupon) as price,
count(*) as cnt
from {$g5['g5_shop_order_table']}
where SUBSTRING(od_time, 1, 10) = '$date'
and ( od_cart_coupon > 0 or od_coupon > 0 or od_send_coupon > 0 ) ";
$row = sql_fetch($sql);
$info['쿠폰']['price'] = (int)$row['price'];
$info['쿠폰']['count'] = (int)$row['cnt'];
return $info;
}
?>
이 함수를 월 별로 바꿔야 할거 같은데 어떻게 SQL을 짜야될지 잘 모르겟습니다.. ㅠㅠ
답변 2개
채택된 답변
+20 포인트
넓은마인드
3년 전
각함수를 호출할때 $date 변수를 월을 넘기시고 (ex. 2022-01)
각 쿼리 SUBSTRING(od_time, 1, 10) = '$date' 이부분을
SUBSTRING(od_time, 1, 7) = '$date'
로 변경하시면 됩니다
로그인 후 평가할 수 있습니다
댓글을 작성하려면 로그인이 필요합니다.
3년 전
건수가 적다면 상관없지만
아니면 이런 식으로 하면 느려집니다.
최소 od_time이 포함되는 인덱스가 필요하고
where SUBSTRING(od_time, 1, 10) = '$date'
이런 식으로 인덱스를 무용지물로 만드는 조건식은 피해야 합니다.
로그인 후 평가할 수 있습니다
답변에 대한 댓글 1개
3년 전
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인
{
global $g5;
$sql = " select sum(od_cart_price + od_send_cost + od_send_cost2) as orderprice,
sum(od_cancel_price) as cancelprice
from {$g5['g5_shop_order_table']}
where SUBSTRING(od_time, 1, 10) = '$date' ";
$row = sql_fetch($sql);
$info = array();
$info['order'] = (int)$row['orderprice'];
$info['cancel'] = (int)$row['cancelprice'];
return $info;
}
를 한 달간으로 바꾸어 보면
function get_order_monthly_sum($date)
{
global $g5;
$from_date= date( 'Y-m-01', $tmp=strtotime( $date);
$to_date= date( 'Y-m-t', $tmp);
$sql = " select sum(od_cart_price + od_send_cost + od_send_cost2) as orderprice,
sum(od_cancel_price) as cancelprice
from {$g5['g5_shop_order_table']}
where od_time between '{$from_date}' and '{$to_date}' ";
$row = sql_fetch($sql);
$info = array();
$info['order'] = (int)$row['orderprice'];
$info['cancel'] = (int)$row['cancelprice'];
return $info;
}
g5_shop_order에 od_time를 포함하는 인데스를 만들어 주세요.