MySQL Getting around error 1093

后端 未结 5 1043
伪装坚强ぢ
伪装坚强ぢ 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

    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.
    

提交回复
热议问题