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); ?>
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;
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;
remove
delete from p, i using projects p, images i where p.p_id = i.p_id and p.p_id = 1;
result
select * from projects; -- +------+ -- | p_id | -- +------+ -- | 2 | -- | 3 | -- +------+ select * from images; -- +------+------+ -- | i_id | p_id | -- +------+------+ -- | 4 | 2 | -- | 5 | 2 | -- | 6 | 3 | -- | 7 | 3 | -- +------+------+
works with pleasure!
source share