Delete with Join in MySQL

后端 未结 14 1007
鱼传尺愫
鱼传尺愫 2020-11-22 06:18

Here is the script to create my tables:

CREATE TABLE clients (
   client_i INT(11),
   PRIMARY KEY (client_id)
);
CREATE TABLE projects (
   project_id INT(         


        
14条回答
  •  终归单人心
    2020-11-22 07:17

    Another method of deleting using a sub select that is better than using IN would be WHERE EXISTS

    DELETE  FROM posts
    WHERE   EXISTS ( SELECT  1 
                     FROM    projects
                     WHERE   projects.client_id = posts.client_id);
    

    One reason to use this instead of the join is that a DELETE with JOIN forbids the use of LIMIT. If you wish to delete in blocks so as not to produce full table locks, you can add LIMIT use this DELETE WHERE EXISTS method.

提交回复
热议问题