MySQL将DELETE FROM与子查询作为条件

时间:2010-12-17 14:15:48

标签: mysql sql subquery sql-delete in-subquery

我正在尝试这样的查询:

DELETE FROM term_hierarchy AS th
WHERE th.parent = 1015 AND th.tid IN (
    SELECT DISTINCT(th1.tid)
    FROM term_hierarchy AS th1
    INNER JOIN term_hierarchy AS th2 ON (th1.tid = th2.tid AND th2.parent != 1015)
    WHERE th1.parent = 1015
);

正如你可能知道的那样,如果同一个tid有其他父母,我想删除1015的父关系。但是,这会产生语法错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS th
WHERE th.parent = 1015 AND th.tid IN (
  SELECT DISTINCT(th1.tid)
  FROM ter' at line 1

我检查了文档,并自行运行子查询,这一切似乎都要检查。谁能弄清楚这里有什么问题?

更新:如下所述,MySQL不允许您删除的表在子查询中用于该条件。

9 个答案:

答案 0 :(得分:239)

对于那些在使用子查询时发现这个问题想要删除的人,我给你留下了这个例子来说明MySQL(即使有些人似乎认为无法完成):

DELETE e.*
FROM tableE e
WHERE id IN (SELECT id
             FROM tableE
             WHERE arg = 1 AND foo = 'bar');

会给你一个错误:

ERROR 1093 (HY000): You can't specify target table 'e' for update in FROM clause

但是这个查询:

DELETE e.*
FROM tableE e
WHERE id IN (SELECT id
             FROM (SELECT id
                   FROM tableE
                   WHERE arg = 1 AND foo = 'bar') x);

可以正常使用:

Query OK, 1 row affected (3.91 sec)

将子查询包含在另一个子查询中(此处名为x),MySQL将很乐意按您的要求进行操作。

答案 1 :(得分:32)

您无法指定要删除的目标表。

解决方法

create table term_hierarchy_backup (tid int(10)); <- check data type

insert into term_hierarchy_backup 
SELECT DISTINCT(th1.tid)
FROM term_hierarchy AS th1
INNER JOIN term_hierarchy AS th2 ON (th1.tid = th2.tid AND th2.parent != 1015)
WHERE th1.parent = 1015;

DELETE FROM term_hierarchy AS th
WHERE th.parent = 1015 AND th.tid IN (select tid from term_hierarchy_backup);

答案 2 :(得分:32)

别名应包含在DELETE关键字后面:

DELETE th
FROM term_hierarchy AS th
WHERE th.parent = 1015 AND th.tid IN 
(
    SELECT DISTINCT(th1.tid)
    FROM term_hierarchy AS th1
    INNER JOIN term_hierarchy AS th2 ON (th1.tid = th2.tid AND th2.parent != 1015)
    WHERE th1.parent = 1015
);

答案 3 :(得分:7)

您需要在delete语句中再次引用别名,例如:

DELETE th FROM term_hierarchy AS th
....

As outlined here in MySQL docs.

答案 4 :(得分:6)

我以稍微不同的方式接近它,它对我有用;

我需要从引用secure_links表的表中删除conditions,其中不再有任何条件行。一个看家脚本基本上。这给了我错误 - 您无法指定要删除的目标表。

所以在这里寻找灵感我想出了下面的查询,它运作得很好。 这是因为它创建了一个临时表sl1,用作DELETE的引用。

DELETE FROM `secure_links` WHERE `secure_links`.`link_id` IN 
            (
            SELECT
                `sl1`.`link_id` 
            FROM 
                (
                SELECT 

                    `sl2`.`link_id` 

                FROM 
                    `secure_links` AS `sl2` 
                    LEFT JOIN `conditions` ON `conditions`.`job` = `sl2`.`job` 

                WHERE 

                    `sl2`.`action` = 'something' AND 
                    `conditions`.`ref` IS NULL 
                ) AS `sl1`
            )

适合我。

答案 5 :(得分:3)

不是删除中的“in”子句......如果从子查询中返回大量值,效率极低吗?不确定为什么你不会只是内部(或右)从要删除的ID的子查询中反对原始表,而不是我们“in(子查询)”。?

DELETE T FROM Target AS T
RIGHT JOIN (full subquery already listed for the in() clause in answers above) ` AS TT ON (TT.ID = T.ID)

也许它在“MySQL不允许它”中得到解答,但是,它对我来说工作正常提供我确保完全澄清要删除的内容(DELETE T FROM Target AS T)。 Delete with Join in MySQL澄清了DELETE / JOIN问题。

答案 6 :(得分:1)

如果您想使用2个查询执行此操作,则可以始终执行与此类似的操作:

1)从表中抓取ID:

SELECT group_concat(id) as csv_result FROM your_table WHERE whatever = 'test' ...

然后用鼠标/键盘或编程语言将结果复制到以下XXX:

2) DELETE FROM your_table WHERE id IN ( XXX )

也许你可以在一个查询中执行此操作,但这是我更喜欢的。

答案 7 :(得分:0)

@ CodeReaper,@ BennyHill: 它按预期工作。

但是,我想知道表中具有数百万行的时间复杂度吗?显然,要在正确索引的表上拥有5k条记录,大约要花费5ms才能执行。

我的查询

SET status = '1'
WHERE id IN (
    SELECT id
    FROM (
      SELECT c2.id FROM clusters as c2
      WHERE c2.assign_to_user_id IS NOT NULL
        AND c2.id NOT IN (
         SELECT c1.id FROM clusters AS c1
           LEFT JOIN cluster_flags as cf on c1.last_flag_id = cf.id
           LEFT JOIN flag_types as ft on ft.id = cf.flag_type_id
         WHERE ft.slug = 'closed'
         )
      ) x)```

Or is there something we can improve on my query above?

答案 8 :(得分:0)

您可以通过这种方式在删除语句上使用别名

DELETE  th.*
FROM term_hierarchy th
INNER JOIN term_hierarchy th2 ON (th1.tid = th2.tid AND th2.parent != 1015)
WHERE th.parent = 1015;