Mysql更新select语句的结果

时间:2015-03-18 09:30:44

标签: mysql

我有两个Joined select语句,我想要交换排名。我加入的select语句是:

SELECT l.id, r.id, l.rank, r.rank, l.oldurl, r.oldurl, l.newurl, r.newurl
    FROM (SELECT * FROM as795_sh404sef_urls WHERE newurl REGEXP 'Itemid\=108' AND rank<>0 GROUP BY oldurl HAVING MAX(id)) AS l
        JOIN (SELECT * FROM as795_sh404sef_urls WHERE rank=0 GROUP BY oldurl) AS r
            ON l.oldurl=r.oldurl;

我正在尝试创建一个更新语句,它将交换排名。只是说l.rank应该在所有情况下都变为0,在这种情况下r.rank应该变成什么是l.rank

UPDATE (SELECT id FROM as795_sh404sef_urls WHERE rank=0 AND oldurl='zumba-gold-8.html' GROUP BY oldurl) r
    JOIN (SELECT * FROM as795_sh404sef_urls WHERE newurl REGEXP 'Itemid\=108' AND rank<>0 AND oldurl='zumba-gold-8.html' GROUP BY oldurl HAVING MAX(id)) l
        ON l.oldurl=r.oldurl
            SET r.rank= l.rank, l.rank=0;   

使用此更新语句,我得到r和l表无法更新。

1 个答案:

答案 0 :(得分:0)

来自documentation

目前,您无法更新表并从子查询中的同一表中进行选择。也许这会在未来版本中发生变化但尚未发生。