美文网首页MySql
mysql You can't specify target t

mysql You can't specify target t

作者: Rinaloving | 来源:发表于2023-01-08 14:13 被阅读0次

mysql 不能对同一个语句进行查询又更新

问题

  • 涉及表(字段)


    涉及表字段.png
  • 执行语句

UPDATE `TbBookStock`  SET nState = 2  WHERE pkCode IN ( SELECT pkCode FROM `TbBookStock` WHERE nState = 5 AND DATE_ADD(tOffShelf,INTERVAL '48' HOUR)  <= NOW() )

  • 错误提示
    You can't specify target table 'TbBookStock' for update in FROM clause
更新.png

解决

  • 修改语句
UPDATE `TbBookStock`  SET nState = 2  WHERE pkCode IN ( SELECT pkCode FROM ( SELECT pkCode FROM `TbBookStock` WHERE nState = 5 AND DATE_ADD(tOffShelf,INTERVAL '48' HOUR)  <= NOW()) AS a )

  • 执行结果


    更新成功.png

参考文档

相关文章

网友评论

    本文标题:mysql You can't specify target t

    本文链接:https://www.haomeiwen.com/subject/vosucdtx.html