0

好的,所以我是派生表的新手。当我运行这个查询时,它给了我一个 MYSQL ERROR 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'
            )
        )
4

2 回答 2

3

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

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;

也就是说,我认为这相当于只做joinon table2

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

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

于 2014-11-19T00:23:27.707 回答
2

您忘记了表别名

delete from table_1
where thing_id in (
    select tid from(
        select table_1.thing_id as tid
        FROM table_1
        inner join table_2
        on table_1.thing_id = table_2.thing_id
        where table_1.stuff = 'stuff'
        ) tmp
    )
于 2014-11-18T23:31:39.587 回答