mysql在把子查询结果作为删除表中数据的条件,mysql不允许在子查询的同时删除原表数据

在上一文中发布了多表删除指定记录,发现达不到我想要的效果,找了很多资料,发现以下方法。

数据库不能边查询边删除,

尝试以下操作

delete  from push_msg_overview where id in (
     select  id from push_msg_overview  where push_date+offset_day >= 20181031  
)

在该 sql 语句中由于子查询中包含 push_msg_overview 表,但我们同时要删除 push_msg_overview 表中的记录,所以允许该语句时报如下错误:

1093-You can't specify target table 'push_msg_overview' for update in FROM clause.

我们的一般思路就是,1、把子查询的结果创建临时表存储。2、把这个临时表作为原表删除的条件。3、删除表数据。

实例:


delete  from push_msg_overview where id in (
  select e. id  from (
    select id from push_msg_overview  where push_date+offset_day >= 20181031  
    ) as e
)

拓展:in常用于where表达式中,其作用是查询某个范围内的数据。

当查询匹配的数据只有一个时可用 = 代替 in,

实践出真知。

本文资料转自CSDN https://blog.csdn.net/qq_26249609/article/details/83615316

posted @ 2020-06-17 09:07  惊鸿难定  阅读(1110)  评论(0编辑  收藏  举报