大成小鱼 2021-10-26 10:08 采纳率: 100%
浏览 30
已结题

mysql中delete删除数据,为何如下第一种写法报错,但是第二种就可以了呢?

1、删除表中的数据,第一次写如下:

[SQL]delete  from yizhu_zhixing_plan_expire 
where plan_id in (
select t.plan_id from yizhu_zhixing_plan_expire  t
 where  yizhu_plan_date>=date_format(now() ,'%Y-%m-%d')
and EXISTS (select 1 from yizhu_basic_info a where a.zuhao_id=t.zuhao_id and a.yizhu_State='开始执行' -- and a.type='长期' 
    )
)

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



报错如上:
2、修改脚本后,执行成功了


[SQL]delete  from yizhu_zhixing_plan_expire 
where plan_id in (
select m.plan_id from (
select t.*from yizhu_zhixing_plan_expire  t
 where  yizhu_plan_date>=date_format(now() ,'%Y-%m-%d')
and EXISTS (select 1 from yizhu_basic_info a where a.zuhao_id=t.zuhao_id and a.yizhu_State='开始执行'  
    ) ) m
)

受影响的行: 0
时间: 2.008s

这个是为啥呢?求指导1

  • 写回答

1条回答 默认 最新

报告相同问题?

问题事件

  • 系统已结题 11月3日
  • 已采纳回答 10月26日
  • 创建了问题 10月26日