2026, 새로운 도약을 시작합니다.

테이블 join 질문드립니다. 채택완료

Copy
<table>
        <thead>
        <tr>
            <th>순위</th>
            <th>총판장</th>
            <th>구매품목</th>
            <th>판매품목</th>
            <th>총 구매금액</th>
            <th>총 판매금액</th>
        </tr>
        </thead>
        <tbody>
        <?
        $sum_count = "0";
        $sql = "
            select a.mb_id
            , count(*) as cnt, group_concat(a.wr_12 separator '||') wr_12a, group_concat(b.wr_12 separator '||') wr_12b, sum(a.wr_31) as tota, sum(b.wr_31) as totb
            , (select c.mb_name from g5_member c where a.mb_id = c.mb_id) mb_name
            from g5_write_csorder a
            inner join g5_write_cscenter b on a.mb_id = b.mb_id
            where
            a.wr_29 between '{$stx1}' and '{$stx2}' and a.wr_50 like '%{$stx7}%' and NOT a.wr_54 IN ('exchange') and
            b.wr_29 between '{$stx1}' and '{$stx2}' and b.wr_50 like '%{$stx7}%'
            group by a.mb_id ORDER BY tota DESC
        ";
        $result = sql_query($sql, TRUE);
        while ($row = sql_fetch_array($result)) {
        $list_namea = explode('||',$row['wr_12a']);
        $numa = array_count_values($list_namea);

        $list_nameb = explode('||',$row['wr_12b']);
        $numb = array_count_values($list_nameb);

        $sum_count ++
        ?>

        <tr>
            <td class="td_c"><?php echo $sum_count ?>위</td>
            <td class="td_c"><?php echo $row['mb_name'] ?></td>
            <td class="td">
            <?php foreach( $numa as $key => $value ){ ?>
            <?php echo $key; ?>(<?php echo $value?>),
            <?php } ?>
            </td>
            <td class="td">
            <?php foreach( $numb as $key => $value ){ ?>
            <?php echo $key; ?>(<?php echo $value?>),
            <?php } ?>
            </td>
            <td class="td_r"><?php echo number_format(str_replace(',', '', $row['tota']) / 2) ?>원</td>
            <td class="td_r"><?php echo number_format(str_replace(',', '', $row['totb']) / 2) ?>원</td>
        <?php } ?>
        </table>

g5_write_csorder(구매)의 제품명과 개수, 금액은 제대로 가져오는데

g5_write_cscenter(판매) 의 제품개수와 금액(totb)이 맞질 않네요... 품목은 맞는데... 

어떻게 해야 제대로 된 값이 나올까요?

답변 1개

원하시는 것을 설명해 주셔야 합니다.

로그인 후 평가할 수 있습니다

답변에 대한 댓글 2개

sum(b.wr_31) as totb 의 값과
<?php foreach( $numb as $key => $value ){ ?>
<?php echo $key; ?>(<?php echo $value?>),
<?php } ?>
의 value값이 맞지가 않아서 질문드렸습니다.
$list_nameb = explode('||',$row['wr_12b']);
$numb = array_count_values($list_nameb);
foreach( $numa as $key => $value ){
echo $key,': ',$value;

어떤 값이 있는지 보여 주셔야 value 값이 안 맞는 것인지 살펴 볼 수 있습니다.

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인
🐛 버그신고