Delete with Join in MySQL

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

提交回复
热议问题