当前位置:网站首页>MySQL delete from and subquery as conditions

MySQL delete from and subquery as conditions

2022-06-22 08:00:00 xiaoke815

I am trying such a query :

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
);

You can tell me , If the same tid Have other parents , I want to delete the parent relationship 1015. however , This will give me a grammatical error :

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

I've checked the document , And run the subquery by yourself , All this seems to have to be checked out . Can anyone figure out what's wrong here ?

to update : Answer as follows ,MySQL Tables to be deleted are not allowed in subqueries .

The best solution

You cannot specify the target table to delete .

resolvent

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);

Second best solution

For those who want to remove this problem when using subqueries , I give you this example to illustrate MySQL The advantages of ( Even if some people seem to think it can't be done ):

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

Will give you a mistake :

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

But this query :

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

Will work well :

Query OK, 1 row affected (3.91 sec)

Include subqueries in an additional subquery ( It's called x) in ,MySQL Will be happy to do what you ask .

The third solution

DELETE The keyword should be followed by an alias :

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
);

The fourth option

You need to delete The alias is referenced again in the statement , Such as :

DELETE th FROM term_hierarchy AS th
....

As outlined here in MySQL docs.

The fifth option

I approach this in a slightly different way , It works for me ;

I need to quote from conditions Delete... From the table of the table secure_links, There are no longer any condition lines . A chore script basically gave me the wrong – You cannot specify the target table to delete .

So find inspiration here , I came up with the following query , It works properly . This is because it creates a file to be used as DELETE Temporary table for reference sl1.

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`
            )

Work for me

reference

原网站

版权声明
本文为[xiaoke815]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220530339647.html