주문현황 월별 출력

주문현황 월별 출력

QA

주문현황 월별 출력

본문

 

3718050134_1643184037.6676.png

 

 

그래프와 결제 수단 주문현황을 월별로 출력을 하려고 합니다

 

코드를 보니


<?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

각함수를 호출할때 $date 변수를 월을 넘기시고 (ex. 2022-01)

각 쿼리 SUBSTRING(od_time, 1, 10) = '$date'  이부분을

 

SUBSTRING(od_time, 1, 7) = '$date'

 

로 변경하시면 됩니다

건수가 적다면 상관없지만

아니면 이런 식으로 하면 느려집니다.

최소 od_time이 포함되는 인덱스가 필요하고

where SUBSTRING(od_time, 1, 10) = '$date'

이런 식으로 인덱스를 무용지물로 만드는 조건식은 피해야 합니다.

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_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를 포함하는 인데스를 만들어 주세요.

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

회원로그인

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