Mysql排名更新

时间:2013-12-09 08:55:19

标签: php mysql sql

伙计们我是新来的,也是MySQL的新手......

所以我正在尝试创建一个管理团队记录的数据库。数据库包含一个名为'team'的表,其中包含一组列,如下所示

 1. TeamID[Primarykey,A_I]
 2. TeamName[Unique]
 3. TeamWins
 4. TeamLoss
 5. TeamPoints

因此,议程是根据积分对团队进行排名,积分越高得分越高。我想要做的是将TeamName,TeamWins,TeamLoss和TeamPoints作为输入通过php,并随着TeamPoints的增加/减少一个团队,它应该向上/向下移动等级并显示排名表。

请帮帮我,谢谢!

2 个答案:

答案 0 :(得分:2)

你可以在这样的SQL中获得等级: -

SELECT TeamID,
        TeamName,
        TeamWins,
        TeamLoss,
        TeamPoints,
        @Rank := @Rank + 1 AS TeamRnk
FROM SomeTable
CROSS JOIN (SELECT @Rank:=0) Sub0
ORDER BY TeamPoints DESC

如果您想要特定团队的排名,那么您可以将其扩展为: -

SELECT TeamID,
        TeamName,
        TeamWins,
        TeamLoss,
        TeamPoints,
        TeamRnk
FROM
(
    SELECT TeamID,
            TeamName,
            TeamWins,
            TeamLoss,
            TeamPoints,
            @Rank := @Rank + 1 AS TeamRnk
    FROM SomeTable
    CROSS JOIN (SELECT @Rank:=0) Sub0
    ORDER BY TeamPoints DESC
) Sub1
WHERE TeamID = xxxxx

答案 1 :(得分:0)

您的查询:

select @rowRank := @rowRank+1 AS rank, team.* from team
JOIN    (SELECT @rowRank := 0) r
order by TeamPoints desc

Sql Fiddle:http://sqlfiddle.com/#!2/7771e/1