2013-04-29 1 views
2

각 팀이 얻은 점수, 목표 차이 및 목표가있는 순위표가 있습니다. 다음은 테이블 구조와 데이터입니다.mySQL 및 VIEW 생성 순위 결정하기

CREATE TABLE standings (
    team_id int(3) unsigned NOT NULL AUTO_INCREMENT, 
    points int(2) unsigned DEFAULT 0, 
    goal_difference int(2) unsigned DEFAULT 0, 
    goals_for int(2) unsigned DEFAULT 0, 
    PRIMARY KEY (team_id) 
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1; 

insert into standings(team_id,points,goal_difference,goals_for) values (1,20,2,17); 
insert into standings(team_id,points,goal_difference,goals_for) values (2,14,8,15); 
insert into standings(team_id,points,goal_difference,goals_for) values (3,9,2,11); 
insert into standings(team_id,points,goal_difference,goals_for) values (4,14,10,12); 
insert into standings(team_id,points,goal_difference,goals_for) values (5,17,10,19); 
insert into standings(team_id,points,goal_difference,goals_for) values (6,5,-11,7); 
insert into standings(team_id,points,goal_difference,goals_for) values (7,14,10,10); 
insert into standings(team_id,points,goal_difference,goals_for) values (8,9,2,14); 
insert into standings(team_id,points,goal_difference,goals_for) values (9,12,1,10); 
insert into standings(team_id,points,goal_difference,goals_for) values (10,9,2,14); 
commit; 

나는 점, goal_difference 및 goals_for의 내림차순으로이 테이블을 정렬 순서에 따라 각 팀에 순위를 할당 할. mySQL에는 RANK 함수가 없으므로이 사이트를 검색 한 후에 이이 쿼리와 함께 나타납니다.

SELECT CASE 
      WHEN @prev_value = concat(points,'-',goal_difference,'-',goals_for) 
      THEN 
      @cur_rank 
      WHEN @prev_value := concat(points,'-',goal_difference,'-',goals_for) 
      THEN 
      @cur_rank := @cur_rank + 1 
     END 
      AS rank, s.team_id, s.points, s.goal_difference, s.goals_for 
    FROM standings s, (SELECT @cur_rank := 0) p, (SELECT @prev_rank := 0) q, (SELECT  @prev_value := NULL) r 
ORDER BY s.points DESC, s.goal_difference DESC, s.goals_for DESC; 

지금까지 그렇게 좋았습니다. 이제 두 가지 질문이 있습니다.

  1. 위의 쿼리 결과를 보면 팀 8과 10 사이가 7 위입니다. 따라서 순위 9를 다음 팀 3에 할당하려고합니다. 쿼리에서 더 이상 열을 추가하지 않고 어떻게이 작업을 수행합니까?
  2. 이 쿼리를 사용하여 VIEW를 만들고 싶습니다. 그러나 mySQL은 하나를 만들고 오류를 제공하지 않습니다 '보기의 SELECT 변수 또는 매개 변수가 포함되어 있습니다'. 이를 위해 VIEW를 만드는 방법을 제안하십시오.

    CREATE VIEW view_standings 
    AS 
        SELECT CASE 
        WHEN @prev_value = concat(points,'-',goal_difference,'-',goals_for) 
        THEN 
         @cur_rank 
        WHEN @prev_value := concat(points,'-',goal_difference,'-',goals_for) 
        THEN 
         @cur_rank := @cur_rank + 1 
        END 
        AS rank,s.team_id,s.points,s.goal_difference,s.goals_for 
    FROM standings s,(SELECT @cur_rank := 0) p,(SELECT @prev_rank := 0) q,(SELECT @prev_value := NULL) r 
    ORDER BY s.points DESC, s.goal_difference DESC, s.goals_for DESC; 
    

답변

2

는 또한 상관 서브 쿼리를 사용하여 순위를 할 수 있습니다. 적당한 양의 데이터를 가지고 있다면 이것은 계산 집약적 일 수 있습니다.

select s.*, 
     (select 1+COUNT(*) 
     from standings s2 
     where s2.points > s.points or 
       (s2.points = s.points and s2.goal_difference > s.goal_difference) or 
       (s2.points = s.points and s2.goal_difference = s.goal_difference and s2.goals_for > sys.goals_for 
     ) as ranking 
from standings s 

from 절에 하위 쿼리 만 있기 때문에이를 뷰로 사용할 수 있습니다.

인덱스가 standings(points, goal_difference, goals_for) 인 경우 성능을 향상시킬 수 있다고 생각합니다.

+0

고든에게 감사드립니다. 이것은 내가 원하는 것입니다. – Noel