使用db_delete()从多个表中删除


Answers:


11

恐怕你不能。

db_delete()使用单个字符串参数来定义要针对哪个表运行查询。一次不能提供多个表。

不过,您可以使用运行任意SQL语句db_query(),例如。

// 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);

db_delete需要一个参数,而不允许一个别名,是烦人。否则,可以使用类似于db_select的JOIN。
Agi Hammerthief 2014年

0

是的,db_delete采用一个参数。您必须db_delete()多次调用才能从多个表中删除数据。请参见以下示例代码:

/**
 * 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();
}

源链接:
https : //github.com/drupal/drupal/blob/7.x/modules/block/block.module
https://api.drupal.org/api/drupal/modules%21block%21block.module/ 7.x

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.