更新表中的排名

时间:2013-12-10 19:52:18

标签: php mysql

此代码即时创建排名,并相应地设置它们。 现在的问题是如何更新表中的Rank值,而不重复?

请帮帮我! 谢谢......

SELECT TeamID,
       TeamName,
       TeamLeader,
       TeamEmail,
       TeamWins,
       TeamLoss,
       TeamPoints,
       TeamRank
       FROM
          (
             SELECT TeamID,
                    TeamName,
                    TeamLeader,
                    TeamEmail,
                    TeamWins,
                    TeamLoss,
                    TeamPoints,
                    @Rank := @Rank + 1 AS TeamRank
                    FROM team
                    CROSS JOIN (SELECT @Rank:=0) Sub0
                    ORDER BY TeamPoints DESC
          ) Sub1

更新[已解决的问题]

我解决了问题,下面是参考解决方案的用户的解决方案

您只是在做一个SELECT语句。为了更新它,你必须在原始表上使用UPDATE语句并使用该查询来提供teamRank的值:

UPDATE team t
INNER JOIN(
       SELECT TeamID,
       TeamPoints,
       @Rank := @Rank + 1 AS TeamRank
       FROM team
       CROSS JOIN (SELECT @Rank:=0) Sub0
       ORDER BY TeamPoints DESC
       ) a ON a.teamID = t.teamID
       SET t.teamRank = a.teamRank

用户:https://stackoverflow.com/users/1385896/filipe-silva"

感谢Filipe Silva ..

2 个答案:

答案 0 :(得分:0)

尝试这样的事情

UPDATE 
SELECT TeamID,
       TeamName,
       TeamLeader,
       TeamEmail,
       TeamWins,
       TeamLoss,
       TeamPoints,
       TeamRank
       FROM
          (
             SELECT TeamID,
                    TeamName,
                    TeamLeader,
                    TeamEmail,
                    TeamWins,
                    TeamLoss,
                    TeamPoints,
                    @Rank := @Rank + 1 AS TeamRank
                    FROM team
                    CROSS JOIN (SELECT @Rank:=0) Sub0
                    ORDER BY TeamPoints DESC
          ) Sub1
SET TeamRank=123
WHERE  TeamID =1

答案 1 :(得分:0)

UPDATE   team, (SELECT @Rank:=0) Sub0
SET      TeamRank = @Rank := @Rank + 1
ORDER BY TeamPoints DESC