You can't specify target table 'A' for update in FROM clause
2015-10-16 15:05
399 查看
mysql中You can't specify target table <tbl> for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。 例如下面这个sql:
delete from tbl where id in
(
select max(id) from tbl a where EXISTS
(
select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1
)
group by tac
)
改写成下面就行了:
delete from tbl where id in
(
select a.id from
(
select max(id) id from tbl a where EXISTS
(
select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1
)
group by tac
) a
)
也就是说将select出的结果再通过中间表select一遍,这样就规避了错误。注意,这个问题只出现于mysql,mssql和oracle不会出现此问题。
delete from tbl where id in
(
select max(id) from tbl a where EXISTS
(
select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1
)
group by tac
)
改写成下面就行了:
delete from tbl where id in
(
select a.id from
(
select max(id) id from tbl a where EXISTS
(
select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1
)
group by tac
) a
)
也就是说将select出的结果再通过中间表select一遍,这样就规避了错误。注意,这个问题只出现于mysql,mssql和oracle不会出现此问题。
相关文章推荐
- Android版本铎A梦幻连连看游戏源代码完整版
- Log4j详细配置
- C#基础
- 你不知道的JavaScript--Item5 全局变量
- CocoaPods安装经历
- spark mllib中ALS算法思想
- RMAN备份与恢复之spfile
- TFB之运行生成定义
- 从Yii2的Request看其CSRF防范策略
- 你不知道的JavaScript--Item5 全局变量
- 设计模式笔记——装饰者模式
- 使用JMeter 完成常用的压力测试
- 题目如下:编写函数 void written_amount(unsigned int amount,char *buffer); 它把amount表示的值转换为单词形式,并
- JasperReports+iReport在eclipse中的使用
- 【转】android 安卓APP获取手机设备信息和手机号码的代码示例
- 基于微信硬件公众平台的智能控制开发流程
- gdb 命令列表
- 勾勒一个Windows窗口程序
- Oracle duplicate搭建dataguard (Backup-based duplication)
- iOS开发UI篇—核心动画简介