当前位置: 首页 > news >正文

MySQL中,不能在一个DML(数据操纵语言,如INSERT, UPDATE, DELETE)语句中直接引用目标表进行子查询

错误示例

   <delete id="deleteOldRelations">DELETE FROM departments_closure_tableWHERE descendant IN ( SELECT descendant FROM departments_closure_tableWHERE ancestor = #{departmentId})</delete>

程序运行之后,会报错:You can't specify target table 'departments_closure_table' for update in FROM clause; nested exception is java.sql.SQLException: You can't specify target table 'departments_closure_table' for update in FROM clause] with root cause

错误原因

这个语句违反了MySQL的语法规则,因为它试图在一个DELETE语句的FROM子句中引用了正在更新的表departments_closure_table。在MySQL中,你不能在一个DML(数据操纵语言,如INSERT, UPDATE, DELETE)语句中直接引用目标表进行子查询。

如何解决

解决办法由ChatGPT生成。
可以通过以下步骤解决这个问题:

  1. 先查询出需要删除的 descendant 列表并存储在临时表中。
  2. 再删除临时表中的这些 descendant

修正后的 SQL

  1. 创建临时表并存储需要删除的 descendant 列表
<insert id="createTempTable" parameterType="long">CREATE TEMPORARY TABLE temp_descendantsASSELECT descendant FROM departments_closure_tableWHERE ancestor = #{departmentId}
</insert>
  1. 删除关系
<delete id="deleteOldRelations">DELETE FROM departments_closure_tableWHERE descendant IN (SELECT descendant FROM temp_descendants)
</delete>
  1. 删除临时表
<delete id="dropTempTable">DROP TEMPORARY TABLE temp_descendants
</delete>

完整的 XML 配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.example.demo.mapper.DepartmentsClosureDao"><insert id="createTempTable" parameterType="long">CREATE TEMPORARY TABLE temp_descendantsASSELECT descendant FROM departments_closure_tableWHERE ancestor = #{departmentId}</insert><delete id="deleteOldRelations">DELETE FROM departments_closure_tableWHERE descendant IN (SELECT descendant FROM temp_descendants)</delete><delete id="dropTempTable">DROP TEMPORARY TABLE temp_descendants</delete></mapper>

Java 调用代码

在 Java 代码中,按顺序调用这些方法:

package com.example.demo.service;import com.example.demo.mapper.DepartmentsClosureDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service
public class DepartmentService {@Autowiredprivate DepartmentsClosureDao departmentsClosureDao;@Transactionalpublic void moveDepartment(Long departmentId, Long newParentId) {// 创建临时表departmentsClosureDao.createTempTable(departmentId);// 删除旧的关系departmentsClosureDao.deleteOldRelations(departmentId);// 插入新的关系 (这个方法需要你自己实现,假设它存在)departmentsClosureDao.insertNewRelations(departmentId, newParentId);// 删除临时表departmentsClosureDao.dropTempTable();}// 其他服务方法
}

总结

通过使用临时表,我们可以避免 MySQL 不允许在同一个查询中更新和查询同一张表的限制。按照上述步骤和示例代码进行实现,可以正确删除部门关系而不引发 You can't specify target table 错误。

http://www.lryc.cn/news/359139.html

相关文章:

  • 【CH32V305FBP6】4. systick 配置
  • 【PECL】在扩展中实现 autoload
  • 企业微信H5授权登录
  • 玩机进阶教程------修改gpt.bin分区表地址段 完全屏蔽系统更新 fast刷写分区表 操作步骤解析【二】
  • Java实现数据结构---数组
  • java解析excel文件,返回json
  • uniapp 添加字体ttf
  • Linux入门攻坚——24、BIND编译安装、Telnet和OpenSSH
  • 1.5.3 基于Java配置方式使用Spring MVC
  • Artifactory清理二进制文件丢失的制品
  • C#中的数组探索
  • 身份认证与口令攻击
  • 卷积网络迁移学习:实现思想与TensorFlow实践
  • Ansible04-Ansible Vars变量详解
  • Flutter 中的 SliverCrossAxisGroup 小部件:全面指南
  • 开源还是闭源这是一个问题
  • 数据结构与算法笔记:基础篇 - 栈:如何实现浏览器的前进和后退功能?
  • 【AIGC】大型语言模型在人工智能规划领域模型生成中的探索
  • 从零开始学习Slam-旋转矩阵旋转向量四元组(二)
  • 基于Spring Security添加流控
  • Python | Leetcode Python题解之第119题杨辉三角II
  • 物联网应用系统与网关
  • 系统稳定性概览
  • Redis-Cluster模式基操篇
  • Golang | Leetcode Golang题解之第113题路径总和II
  • 云计算与 openstack
  • golang语言的gofly快速开发框架如何设置多样的主题说明
  • lynis安全漏洞扫描工具
  • C++ 多重继承的内存布局和指针偏移
  • centos时间不对