MYSQL ERROR 1248 我卡住了

标签 mysql sql derived-table

好的,我是派生表的新手。当我运行这个查询时,它给了我一个 MYSQL 错误 1248。有人知道解决这个问题的方法吗?这可能是我做过的最复杂的查询,我只是想让它工作。谢谢!

delete from table_1
    where thing_id in (
        select tid from(
            select thing_id as tid
            FROM table_1
            inner join table_2
            on table_1.thing_id = table_2.thing_id
            where stuff = 'stuff'
            )
        )

最佳答案

MySQL 通常不允许您在语句的其余部分引用正在删除(或更新)的表。您可以使用嵌套子查询来解决这个问题(正如您似乎正在尝试做的那样)。但是,我认为最好使用显式 join 进行更新:

delete t1
    from table_1 t1 join
         (select t1.thing_id as tid
          from table_1 t1 inner join
               table_2 t2 
               on t1.thing_id = t2.thing_id
          where stuff = 'stuff'
        ) tt
        on t1.thing_id = tt.tid;

也就是说,我认为这等同于对table2 执行join:

delete t1
    from table_1 t1 join
         table_2 t2 
         on t1.thing_id = t2.thing_id;
   where stuff = 'stuff';

后一种方法应该也有更好的性能。

关于MYSQL ERROR 1248 我卡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27006186/

相关文章:

sql - 检查 SQL Server 中的 View

SQL View 与派生表

php - 无法增加/减少 php 中的重载对象或字符串偏移量

php - 如何处理不同时区的 MYSQL NOW()?

SQL将数据插入临时表,拆分列

SQL 服务器 2008 : Insert variable into DML statements using Stored Procedure

Access 中的 SQL 帮助 – 查找数据缺失

mysql - SQL如何计算至少有1,5,10,20等交易的信用卡数量

mysql - Magento 内部连接和排序

php - mysql_real_escape_string 的问题