Deleting rows from multiple tables in MySQL

2020-03-24 04:55发布

Here is what i'm trying to do:

Delete a project from the projects table and all the images associated with that project in the images table.

Lets say $del_id = 10

DELETE FROM projects, images WHERE projects.p_id = '$del_id' AND images.p_id = '$del_id'

What is wrong with this query?

标签: php sql mysql
7条回答
Rolldiameter
2楼-- · 2020-03-24 05:11

The answer

<?php
  $query = sprintf("
  DELETE FROM p, i
  USING projects p, images i
  WHERE p.p_id = %d
    AND p.p_id = i.p_id
  ", $del_id);
?>

The test

projects

create table projects (
  p_id int unsigned not null auto_increment primary key
);
insert into projects (p_id) values (1),(2),(3);
select * from projects;
-- +------+
-- | p_id |
-- +------+
-- |    1 | 
-- |    2 | 
-- |    3 | 
-- +------+

images

create table images (
  i_id int unsigned not null auto_increment primary key,
  p_id int unsigned default null
);
insert into images (p_id) values (1),(1),(1),(2),(2),(3),(3);
select * from images;
-- +------+------+
-- | i_id | p_id |
-- +------+------+
-- |    1 |    1 | 
-- |    2 |    1 | 
-- |    3 |    1 | 
-- |    4 |    2 | 
-- |    5 |    2 | 
-- |    6 |    3 | 
-- |    7 |    3 | 
-- +------+------+

the delete

delete from p, i
using projects p, images i
where p.p_id = i.p_id
 and  p.p_id = 1;

the result

select * from projects;
-- +------+
-- | p_id |
-- +------+
-- |    2 | 
-- |    3 | 
-- +------+

select * from images;
-- +------+------+
-- | i_id | p_id |
-- +------+------+
-- |    4 |    2 | 
-- |    5 |    2 | 
-- |    6 |    3 | 
-- |    7 |    3 | 
-- +------+------+

works a treat!

查看更多
ゆ 、 Hurt°
3楼-- · 2020-03-24 05:13
$sql = "DELETE FROM projects, images WHERE projects.p_id = '".$del_id."' or images.p_id = '".$del_id."'";

When being deleted, an item will never meet both of these requirements, therefore it must be OR not AND

查看更多
聊天终结者
4楼-- · 2020-03-24 05:15

(Wrong answer, MySQL allows this)

You can't delete from two tables in one query.

The closest you can get is wrap the two deletes in a transaction:

begin transaction
delete from projects where p_id = $del_id
delete from images where p_id = $del_id
commit transaction
查看更多
够拽才男人
5楼-- · 2020-03-24 05:16

As Chacha102 noted, the problem of your query was the AND in the WHERE clause.

However, you may want to use the JOIN syntax for multi-table DELETEs, which I find easier to read:

DELETE     projects, images
FROM       projects 
LEFT JOIN  images ON images.p_id = projects.p_id
WHERE      projects.p_id = 10;
查看更多
我想做一个坏孩纸
6楼-- · 2020-03-24 05:26

Change the AND into an OR.

You might want to use a foreign key constraint with a cascading delete, much easier, but you have to use innoDB and create this FK-constraint. Delete the project and all related images will be deleted as well.

查看更多
对你真心纯属浪费
7楼-- · 2020-03-24 05:28
DELETE projects, images 
FROM projects, images 
WHERE projects.p_id = '$del_id' 
AND projects.p_id = images.p_id;
查看更多
登录 后发表回答