0
UPDATE PlayerLeague l 
JOIN PlayerStat s ON l.player_id=s.player_id 
SET l.league_id=8 
WHERE l.league_id=2 
ORDER BY s.score 
DESC LIMIT 5

我有错误:

{ [Error: ER_WRONG_USAGE: Incorrect usage of UPDATE and ORDER BY] 
code: 'ER_WRONG_USAGE', index: 0 }

这是什么意思?

我试试这个:

UPDATE PlayerLeague l 
SET l.league_id=8 
WHERE l.player_id IN 
(SELECT player_id FROM PlayerStat s 
JOIN PlayerLeague l ON s.player_id=l.player_id 
WHERE l.league_id=2 
ORDER BY s.score 
DESC LIMIT 5)

但这也是错误的。

4

2 回答 2

1

我找到了一个解决方案:

UPDATE PlayerLeague l 
JOIN (SELECT s.player_id 
    FROM PlayerStat s 
    JOIN PlayerLeague l 
    ON s.player_id=l.player_id 
    WHERE l.league_id=2 
    ORDER BY s.score DESC LIMIT 5
) AS temp
ON l.player_id=temp.player_id
SET l.league_id=8 
于 2013-10-12T05:32:10.197 回答
-1

我不确定您是否打算通过使用 LIMIT 或检索具有高分的 TOP 5 playerID 然后使用该 playerID 更新所有行来限制仅更新几行。

如果是第一种情况,则 LIMIT 应该在 JOIN 之外。你应该检查正确的语法。但如果是第二种情况,那么这里是查询。

    update  PlayerLeague pl
    JOIN 
    (
        select t1.player_id
        from PlayerLeague t1
        join PlayerStat t2
            on t1.player_id=t2.player_id
            and t1.league_id=2
        order by t2.score desc
        limit 5
    )ps
    ON pl.player_id=ps.player_id
    set  pl.league_id=8
于 2013-10-12T06:03:51.907 回答