MySQL Getting around error 1093

后端 未结 5 948
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 03:21

Error 1093 states that you can\'t UPDATE or DELETE using a subquery if your subquery queries the table you are deleting from.

So you can\'t do

delete         


        
相关标签:
5条回答
  • 2020-12-07 03:43

    A workaround, found in http://bugs.mysql.com/bug.php?id=6980, that worked for me is to create an alias to the sub query that will return the items. So

    delete from table1 where id in 
      (select something from table1 where condition)
    

    would be changed to

    delete from table1 where id in
      (select p.id from (select something from table1 where condition) as p)
    
    0 讨论(0)
  • 2020-12-07 03:43

    Simplified:

    -- Collect ids
    CREATE TEMPORARY TABLE cleanup_lookup AS 
    SELECT id FROM table1 WHERE condition;
    
    -- Delete the selected records
    DELETE t FROM table1 t INNER JOIN cleanup_lookup l ON t.id = l.id;
    
    -- Temporary tables get dropped when the connection is closed.
    
    0 讨论(0)
  • 2020-12-07 03:47

    Currently, you cannot delete from a table and select from the same table in a subquery - details

    You just cannot cannot specify target table for delete

    one of my workaround : MySQL DELETE FROM with subquery as condition

    0 讨论(0)
  • 2020-12-07 03:57

    You can use this one without hesitation.

    Your query:

    delete from table1 
    where id in (select something from table1 where condition);
    

    Updated query:

    DELETE FROM table1 
    WHERE id IN (SELECT * 
                 FROM 
                     (SELECT MIN(id) FROM table1 GROUP BY Column2) x);
    

    Here Column2 is column on which you want to find duplicate records.

    0 讨论(0)
  • 2020-12-07 04:01

    You can do

    delete t1,t2 
    FROM  table1 t1  
    INNER JOIN 
    table1 t2 ON (t2.something = t1.id);
    

    For the query in the question, this should be equivalent:

    delete A
    from adjacencies A
    join adjacencies B ON A.parent = B.parent AND B.child=@me AND B.parent != @me
    join adjacencies C ON A.child = C.child AND C.parent=@me
    
    0 讨论(0)
提交回复
热议问题