文章目录

  • 1. 复现错误
  • 2. 分析错误
  • 3. 解决错误

1. 复现错误

今天在工作时,接到一个新需求,就是将app_page_button表中的label_code字段修改为edit,条件如下:

  1. 只更新值为nulllabel_code

  2. 且以/edit/${id}'结尾的option_value

首先使用如下SQL查询满足上述条件的记录,如下代码所示:

SELECT id, label, label_code, option_value
FROM app_page_button
WHERE label_code IS NULL AND option_value LIKE '%/edit/${id}';+-----+-------+------------+-----------------------+
| id  | label | label_code | option_value          |
+-----+-------+------------+-----------------------+
| 706 | 编辑  | NULL       | put:common/edit/${id} |
| 710 | 编辑  | NULL       | put:common/edit/${id} |
| 714 | 编辑  | NULL       | put:common/edit/${id} |
+-----+-------+------------+-----------------------+

得到满足上述条件的记录有3条,使用如下SQL语句修改:

UPDATE app_page_button
SET label_code = 'edit'
WHEREid IN ( SELECT id FROM  app_page_button WHERE label_code IS NULL AND option_value LIKE '%/edit/${id}' );ERROR 1093 (HY000): You can't specify target table 'app_page_button' for update in FROM clause

You can't specify target table 'app_page_button' for update in FROM clause的错误。

2. 分析错误

最近申请的文心一言刚审核通过,可以借助它来分析我的错误,如下图所示:

文心一言的回答,和我的本意不一致。我本想在查询结果中,更新label_code值。

因而,我需要自己分析,来解决这个错误。

You can't specify target table 'app_page_button' for update in FROM clause的含义:不能在同一表(app_page_button)中查询的数据,作为同一表(app_page_button)的更新数据。

3. 解决错误

既然不能先select出同一表中的某些值,再update这个表(在同一语句中),那就采用将查询结果存储到临时表(tmp)表中,id从这个临时表(tmp)中获取,如下代码所示:

UPDATE app_page_button
SET label_code = 'edit'
WHEREid IN ( SELECT id FROM ( SELECT id FROM app_page_button WHERE label_code IS NULL AND option_value LIKE '%/edit/${id}' ) as tmp );Query OK, 3 rows affected (0.01 sec)
Rows matched: 3  Changed: 3  Warnings: 0

根据Query OK, 3 rows affected (0.01 sec)这句话可知,已更新成功,从如下SQL可以看到:

select id, label, label_code, option_value
from app_page_button
where id in (706,710,714);+-----+-------+------------+-----------------------+
| id  | label | label_code | option_value          |
+-----+-------+------------+-----------------------+
| 706 | 编辑  | edit       | put:common/edit/${id} |
| 710 | 编辑  | edit       | put:common/edit/${id} |
| 714 | 编辑  | edit       | put:common/edit/${id} |
+-----+-------+------------+-----------------------+
3 rows in set (0.00 sec)

全网详细解决1093 - You can‘t specify target table ‘xxx‘ for update in FROM clause的错误相关推荐

  1. [Err] 1093 - You can't specify target table 'xxx' for update in FROM clause解决方法

    执行开发同学提供的删除数据的sql时报错[Err] 1093 - You can't specify target table 'run_result' for update in FROM clau ...

  2. 记录一个多表查询的问题 #1093 - You can‘t specify target table ‘xxx‘ for update in FROM clause

    SQL里面,多表查询比较常见,用法也比较简单 可以是 SELECT ood.oodId, ood.status, ood.userId, orders.createdAt FROM ood, orde ...

  3. 【MySQL】Error Code: 1093. You can‘t specify target table ‘xxx‘ for update in FROM clause

    1. 报错信息: Error Code: 1093. You can't specify target table 'self_check' for update in FROM clause 错误的 ...

  4. 解决——》[Err] 1093 - You can't specify target table 'fcf' for update in FROM clause

    版权声明:本文为博主原创文章,无需授权即可转载,甚至无需保留以上版权声明,转载时请务必注明作者. https://blog.csdn.net/weixin_43453386/article/detai ...

  5. [Err] 1093 - You can't specify target table 's' for update in FROM clause

    [Err] 1093 - You can't specify target table 's' for update in FROM clause 执行SQL DELETE from book WHE ...

  6. MySQL [1093] You can‘t specify target table ‘titles_test‘ for update in FROM clause

    执行以下语句: delete from titles_test where id not in(select min(id)from titles_testgroup by emp_no); 会报出错 ...

  7. MySQL 语法问题:You can‘t specify target table ‘xxx‘ for update in FROM clause. 原因及解决方法

    报错信息如下: [Code: 1093, SQL State: HY000] You can't specify target table 'bd_bankaccbas' for update in ...

  8. 错误:You can't specify target table 'xxx' for update in FROM clause的解决

    今天在MySQL数据库删除重复数据的时候遇到了一个问题.如下脚本: DELETE FROM tempA WHERE tid IN ( SELECT MAX(tid) AS tid FROM tempA ...

  9. 【mysql 】sql错误代码 1093 You cannot specify target table xxxx for update in FROM clause

    在mysql8.X中执行如下语句: DELETE FROM`test`.unsign_wocode WHEREid IN (SELECTa.id FROM`test`.unsign_wocode a, ...

最新文章

  1. 昨天,JetBrains 推出“下一代 IDE”,快看有哪些值得期待的功能!
  2. 使用流光法实现物体跟踪
  3. 线性代数学习笔记(几何版)
  4. lazada开店流程图解,及平台类目佣金分享!
  5. Linux定时任务服务crond
  6. android 怎么初始化下拉框_第30讲:“二师兄”的成长历程之二,类属性的初始化...
  7. 第7章 C控制语句:分支和跳转
  8. 如何通过VC的 CHttpFile 抓取网页内容
  9. 为什么我使用Java
  10. 动态规划——打家劫舍(Leetcode 198)
  11. SQL Server 2008 各种DateTime的取值范围
  12. 枚举类 enum,结构体类 struct
  13. 微设计(www.weidesigner.com)介绍系列文章(三)
  14. 2017 计蒜之道 初赛 第五场 A. UCloud 机房的网络搭建
  15. 栅栏密码--Python解密脚本
  16. 虚拟仿真实验项目所需服务器,工程结构虚拟仿真实验室
  17. 解构语音交互产品--人工智能AI技术
  18. Initialization of variable was never used; consider replacing with assignment to ‘_’ or removing it
  19. 电脑重新分区后文件怎么恢复?流水的难题铁打的办法
  20. python中多重if语句用法_python-循环语句的简单条件语句、多重条件语句和嵌套条件语句编写...

热门文章

  1. 实体类重写toString方法
  2. 排列组合( Lindström–Gessel–Viennot lemma 定理)
  3. Python 复制一份文件
  4. 做男人要象狗,做女人要学猫
  5. 学习谷歌开源工具Magenta
  6. packet tracer安装
  7. 水产养殖微滤机滤网 微滤机滤网
  8. 单片机毕设 STM32的智能水产养殖系统
  9. (六十七)神经网络——MLP
  10. go语言需要web服务器么,使用Go开发web服务器