순위 구하는 sql 문 문의

순위 구하는 sql 문 문의

QA

순위 구하는 sql 문 문의

본문

※ Q&A는 질문에 따른 답변을 받는 곳이지 의뢰를 하는 곳이 아닙니다.
    과제해결 또는 소스를 짜달라고 부탁하거나 답변자에게 막말할 경우 계정 블럭처리 합니다.

위에껀 해당 컬럼의 총 포인트를 구하는 거구요
밑에건 멤버 테이블이랑 포인트 테이블 조인해서 
멤버들의 포인트 순위를 구하는거입니다.

여기서 {$member['mb_id']} 의 po_rel_table 에 대한 포인트 순위를 구할려면 어케해야 하나여?


                                        $po_rel_table = '@member';
$sqlmsum = " select sum(po_point) as po_point_sum, mb_id from {$g5['point_table']} where mb_id = '{$member['mb_id']}' and po_datetime > (now() - INTERVAL 7 DAY) and po_rel_table = '$po_rel_table' ";
$rowmsum = sql_fetch($sqlmsum);
$sql = "SELECT * , COUNT(B.mb_id ) AS count, SUM( B.po_point ) AS point
FROM {$g5['member_table']} as A
LEFT JOIN {$g5['point_table']} as B ON ( A.mb_id = B.mb_id AND B.po_rel_table = '$po_rel_table') where B.po_datetime > (now() - INTERVAL 7 DAY)  group by B.mb_id order by point desc LIMIT 0, 1 ";
$result = sql_query($sql);

이 질문에 댓글 쓰기 :

답변 2

위 코드는 순위내는데는 아무 소용없는 것 같은데요?

순위는 아래처럼 하면 됩니다만 로그인 스킨 같은 곳에 넣어서 페이지 이동할 때 마다  순위를

계산하게 하는 것은 아주 무식한 짓입니다

실시간으로 순위를 내야하는 용도가 아니라면 로그인 할 때(login_check.php) 회원 테이블 여분필드에

구한 등위를 넣어두고 사용하는 방법으로 하면 될 것입니다

그리고 회원수가 제법되고 포인트 내역이 많으면 쓸데없이 join하는 것은 느리게만 만들뿐입니다

 

-- 1000, 500, 500, 300 이게 포인트면 500점은 2등, 300점은 4등이 되어야할테죠?

 

 

$po_rel_table = '@member';

$result = sql_query(" select sum(po_point) as po_point_sum, mb_id from {$g5['point_table']} where po_datetime > (now() - INTERVAL 7 DAY) and po_rel_table = '$po_rel_table' group by mb_id order by po_point_sum desc");

$nn = 0;
while($row=sql_fetch_array($result)){ $nn++;
 if($pre_point != $row[po_point_sum]) $gd =$nn;
  $pre_point  = $row[po_point_sum];
  if($member[mb_id] == $row[mb_id]){ $my_grade = $gd; break;}
}
echo "등위:".$my_grade.'==>'.$member[mb_id];​

 

 

서브쿼리가 된다면 이렇게 해도되겠습니다

 

$row = sql_fetch(" select sum(po_point) as po_tot, mb_id from {$g5['point_table']} where mb_id = '{$member['mb_id']}'  and po_datetime > (now() - INTERVAL 7 DAY) and po_rel_table = '$po_rel_table' ");

$row2 = sql_fetch(" select count(*) as cnt  from (select sum(po_point) as po_point_sum from {$g5['point_table']} where po_datetime > (now() - INTERVAL 7 DAY) and po_rel_table = '$po_rel_table'  group by mb_id) as b where b.po_point_sum > '$row[po_tot]' ");
echo '나의 등위:' .($row2[cnt]+1).'<===> 포인트: '.$row[po_tot];

 

 

 

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

회원로그인

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