Mysql ERROR 1093(HY000)更新一行

时间:2015-08-23 12:10:50

标签: mysql

我想通过更新行的id来更新表(id是自动增量)。这是代码:

 update question_bank set id=1 where id=(select min(id) from question_bank where id>=1);

但我收到了这个错误:

ERROR 1093 (HY000): You can't specify target table 'question_bank' for update in FROM clause

我该怎么办? 谢谢:))

1 个答案:

答案 0 :(得分:1)

如果您的ID是唯一的,您可以使用:

update question_bank qb
    set id = 1
    where id >= 1
    order by id
    limit 1;

或者,您可以使用join

update question_bank qb join
       (select min(id) as minid from question_bank where id>=1) qb2
       on ab.id = qb2.minid
    set qb.id = 1;

或者,您可以使用MySQL技巧解决此问题,但使用其他级别的子查询:

update question_bank
    set id = 1
    where id = (select minid
                from (select min(id) as minid
                      from question_bank
                      where id >= 1
                     ) qb
               );