1

我有一个适用于所有其他数据库系统的简单查询,但在 MySQL 中失败:

UPDATE points p 
SET p.userid = 5224 
WHERE p.userid = 2532 
AND NOT EXISTS (
    SELECT 1
    FROM points q
    WHERE q.userid = 5224
    AND q.game = p.game
)

我收到以下错误消息:

#1093 - You can't specify target table 'p' for update in FROM clause

有什么解决方法吗?

4

2 回答 2

3

您不能在 UPDATE 子句中为主表起别名。这应该有效:

UPDATE points 
SET userid = 5224 
WHERE userid = 2532 
AND NOT EXISTS (
    SELECT 1
    FROM points q
    WHERE q.userid = 5224
    AND q.game = points.game
)
于 2009-11-26T18:10:03.947 回答
2

利用:

UPDATE POINTS
   SET userid = 5224 
 WHERE userid = 2532 
   AND game NOT IN (SELECT q.game
                      FROM POINTS q
                     WHERE q.userid = 5224)
于 2009-11-26T18:14:28.963 回答