I’m trying to combine 2 queries into one like this
$result=$db->query ("DELETE FROM menu WHERE name = '$new'") or die($db->error);
$result=$db->query ("DELETE FROM pages WHERE title = 'new'") or die($db->error);
Into
$result=$db->query ("DELETE FROM menu AS m, pages AS p WHERE m.name = 'new' AND p.title='new'") or die($db->error);
But DB gives syntax error. What’s wrong?
You sure well can delete from multiple tables in one statement with MySQL. Your requirements would work using something like the following query:
Or:
There’s one catch with these examples though: both values must exist for this to work.
There should be more ways to get the desired result I think, without this constraint (using other types of
JOINs I would think) but I haven’t been able to figure out how that should work.If the rows you want to delete are somehow linked to each other with foreign keys (and you are not using foreign key constraints with InnoDB tables), it should be even more easy to do what you want. Something like this should work then:
Be careful with these examples though. Experiment on a test environment first, as I’m not sure 100% of the exact implications to be honest; I just wanted to provide a hint for the fact that deleting from multiple tables at once is possible.