How can we delete fields from multiple tables using db_delete()
and INNER JOIN
in Drupal 7?
2 Answers
You can't I'm afraid.
db_delete()
takes a single string parameter to define which table the query is to be run against. There's no scope to provide more than one table at a time.
You can run any SQL statement you like, though, with db_query()
, eg.
// Deletes all nodes and related author users...please don't actually run this!!
$sql = 'DELETE u, n FROM {node} n INNER JOIN {users} u ON u.uid = n.uid';
db_query($sql);
-
That
db_delete
takes a single parameter, without allowing an alias, is annoying. Otherwise, it would be possible to use JOINs similar to db_select. Commented Mar 28, 2014 at 12:35
Yes, db_delete takes a single parameter. You have to call db_delete()
multiple times to delete data from multiple tables. Please see the following sample code:
/**
* Implements hook_menu_delete().
*/
function block_menu_delete($menu) {
db_delete('block')
->condition('module', 'menu')
->condition('delta', $menu['menu_name'])
->execute();
db_delete('block_role')
->condition('module', 'menu')
->condition('delta', $menu['menu_name'])
->execute();
}
Source link:
https://github.com/drupal/drupal/blob/7.x/modules/block/block.module
https://api.drupal.org/api/drupal/modules%21block%21block.module/7.x