数据库删除重复属性列,错误[HY000][1093] You can't specify target table 'orders' for update in FROM clause
来源:互联网 发布:windows java进程监控 编辑:程序博客网 时间:2024/06/05 17:05
存在一个orders表:
CREATE TABLE `orders` ( `Id_o` int(11) NOT NULL AUTO_INCREMENT, `orderNo` VARCHAR(20) NOT NULL, `Id_p` int(11) DEFAULT NULL, PRIMARY KEY (`Id_O`)) ENGINE=InnoDB DEFAULT CHARSET=utf8
因为存在orderNo重复的行,所以将其删除去。
首先查询出表中orderNo属性重复的orderNo:
SELECT * FROM orders GROUP BY OrderNo HAVING count(OrderNo) >1;
然后利用DELETE 删除:
DELETE FROM orders WHERE orderNo in (SELECT orderNo FROM orders GROUP BY OrderNo HAVING count(OrderNo) >1)
但是IDEA提示报错:
[HY000][1093] You can't specify target table 'orders' for update in FROM clause
检查了半天,语法一点都没错误。
最后网上查找,发现是因为不能select了一个属性列,然后同时UPDATE更新这个属性列所在的表,
这是在MySql才会存在的。
解决方法
用另外一个表,存储SELECT出来重复的orderNo属性列。
但是每次检查删除重复属性的时候,都创建一个表,或者用了之后要DROP,会影响效率,内存等等。
所以可以采用临时表。
当工作在非常大的表上时,你可能偶尔需要运行很多查询获得一个大量数据的小的子集,不是对整个表运行这些查询,而是让MySQL每次找出所需的少数记录,将记录选择到一个临时表可能更快些,然后多这些表运行查询。
创建临时表就是在CREATE 语句加上一个TEMPORARY .
接着上面的问题。
创建一个临时表temp用来保存语句:
SELECT orderNo
FROM orders
GROUP BY OrderNo
HAVING count(OrderNo) > 1; 的查询表结果。
CREATE TEMPORARY TABLE `TEMP` SELECT orderNo FROM orders GROUP BY OrderNo HAVING count(OrderNo) > 1;
这样删除的时候,就可以用上这个temp表,里面保存了orderNo重复属性字段。
DELETE FROM ordersWHERE orderNo IN (SELECT * FROM TEMP);
这样就可以删除掉表里存在指定重复属性的元祖。当然,临时表会在connection/session断开的时候释放。
- 数据库删除重复属性列,错误[HY000][1093] You can't specify target table 'orders' for update in FROM clause
- ERROR 1093 (HY000): You can't specify target table 'b' for update in FROM clause
- ERROR 1093 (HY000): You can't specify target table 'SDS' for update in FROM clause
- [Django](1093, "You can't specify target table 'fee_details_invoices' for update in FROM clause") 错误
- MySQL 错误码: 1093 You can't specify target table 'jc_user' for update in FROM clause
- mysql错误:1093-You can’t specify target table for update in FROM clause的解决方法
- 【mysql 】sql删除重复记录 You can't specify target table '表名' for update in FROM clause
- mysql中You can't specify target table for update in FROM clause错误
- MySQL "You can't specify target table 'X' for update in FROM clause" 错误解决方法
- Mysql 错误 Code: 1093. You can't specify target table for update in FROM clause
- mysql中You can't specify target table for update in FROM clause错误
- mysql中You can't specify target table for update in FROM clause错误
- mysql中You can't specify target table for update in FROM clause错误
- mysql中You can't specify target table for update in FROM clause错误 转
- mysql中You can’t specify target table for update in FROM clause错误解决方法
- mysql中You can\'t specify target table for update in FROM clause错误
- mysql中You can’t specify target table for update in FROM clause错误解决方法
- mysql中You can't specify target table for update in FROM clause错误
- RSA算法
- JSP EL
- JSP Tag
- Elasticsearch搜索引擎
- python 贪心算法实现 斗地主发牌
- 数据库删除重复属性列,错误[HY000][1093] You can't specify target table 'orders' for update in FROM clause
- Serverlet监听器
- [LintCode]102.带环链表
- nyoj 571 整数划分(三)(递归)
- Servlet过滤器
- 简单排序算法总结(C语言版)
- reduceByKeyAndWindow实现基于滑动窗口的热点搜索词实时统计(Java版本)
- JSP 分页
- Gson 的使用和GsonFormat插件的使用