Delete with Join in MySQL

后端 未结 14 1030
鱼传尺愫
鱼传尺愫 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 06:55

    Since you are selecting multiple tables, The table to delete from is no longer unambiguous. You need to select:

    DELETE posts FROM posts
    INNER JOIN projects ON projects.project_id = posts.project_id
    WHERE projects.client_id = :client_id
    

    In this case, table_name1 and table_name2 are the same table, so this will work:

    DELETE projects FROM posts INNER JOIN [...]
    

    You can even delete from both tables if you wanted to:

    DELETE posts, projects FROM posts INNER JOIN [...]
    

    Note that order by and limit don't work for multi-table deletes.

    Also be aware that if you declare an alias for a table, you must use the alias when referring to the table:

    DELETE p FROM posts as p INNER JOIN [...]
    

    Contributions from Carpetsmoker and etc.

    0 讨论(0)
  • 2020-11-22 06:57

    Single Table Delete:

    In order to delete entries from posts table:

    DELETE ps 
    FROM clients C 
    INNER JOIN projects pj ON C.client_id = pj.client_id
    INNER JOIN posts ps ON pj.project_id = ps.project_id
    WHERE C.client_id = :client_id;
    

    In order to delete entries from projects table:

    DELETE pj 
    FROM clients C 
    INNER JOIN projects pj ON C.client_id = pj.client_id
    INNER JOIN posts ps ON pj.project_id = ps.project_id
    WHERE C.client_id = :client_id;
    

    In order to delete entries from clients table:

    DELETE C
    FROM clients C 
    INNER JOIN projects pj ON C.client_id = pj.client_id
    INNER JOIN posts ps ON pj.project_id = ps.project_id
    WHERE C.client_id = :client_id;
    

    Multiple Tables Delete:

    In order to delete entries from multiple tables out of the joined results you need to specify the table names after DELETE as comma separated list:

    Suppose you want to delete entries from all the three tables (posts,projects,clients) for a particular client :

    DELETE C,pj,ps 
    FROM clients C 
    INNER JOIN projects pj ON C.client_id = pj.client_id
    INNER JOIN posts ps ON pj.project_id = ps.project_id
    WHERE C.client_id = :client_id
    
    0 讨论(0)
  • 2020-11-22 06:59
    mysql> INSERT INTO tb1 VALUES(1,1),(2,2),(3,3),(6,60),(7,70),(8,80);
    
    mysql> INSERT INTO tb2 VALUES(1,1),(2,2),(3,3),(4,40),(5,50),(9,90);
    

    DELETE records FROM one table :

    mysql> DELETE tb1 FROM tb1,tb2 WHERE tb1.id= tb2.id;
    

    DELETE RECORDS FROM both tables:

    mysql> DELETE tb2,tb1 FROM tb2 JOIN tb1 USING(id);
    
    0 讨论(0)
  • 2020-11-22 07:04

    Or the same thing, with a slightly different (IMO friendlier) syntax:

    DELETE FROM posts 
    USING posts, projects 
    WHERE projects.project_id = posts.project_id AND projects.client_id = :client_id;
    

    BTW, with mysql using joins is almost always a way faster than subqueries...

    0 讨论(0)
  • 2020-11-22 07:04

    You can also use ALIAS like this it works just used it on my database! t is the table need deleting from!

    DELETE t FROM posts t
    INNER JOIN projects p ON t.project_id = p.project_id
    AND t.client_id = p.client_id
    
    0 讨论(0)
  • 2020-11-22 07:06

    You just need to specify that you want to delete the entries from the posts table:

    DELETE posts
    FROM posts
    INNER JOIN projects ON projects.project_id = posts.project_id
    WHERE projects.client_id = :client_id
    

    EDIT: For more information you can see this alternative answer

    0 讨论(0)
提交回复
热议问题