2013-12-09 75 views
-3

傢伙我在這裏和新MySQL的新太..Mysql的排名更新

所以我想創建用於管理團隊record.The數據庫的數據庫包含一個名爲「團隊」表一組列如下,

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

所以議程是排名積分的基礎上,兩隊的分越大越高rank.All我想要做的是通過採取TeamName,TeamWins,TeamLoss和TeamPoints作爲輸入php,並且隨着TeamPoints隊伍的增加/減少,它應該向上/向下移動隊列並顯示排名表。

請幫幫我,謝謝!

+3

'由TeamPoints DESC'命令? –

+0

TeamPoints或TeamRank –

+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 
+0

感謝man,它在數據庫端工作,但我如何在PHP代碼上實現這一點。 –

+0

這就是你個人球隊的排名,或者是所有球隊的第一名。它將在查詢時執行此操作,因此您可以選擇要顯示的列。不太清楚你想要展示的是什麼,你不能通過基於這個循環進行選擇而得到。 – Kickstart

+0

我不能在這裏發佈代碼,即時創建一個新的問題,你可以看到那裏 –

0

您的疑問:

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

的Sql小提琴:http://sqlfiddle.com/#!2/7771e/1