【问题标题】:Delete 2 tables and WHERE over 3 tables删除 2 个表和 WHERE 超过 3 个表
【发布时间】:2020-09-01 13:48:38
【问题描述】:

我试图寻找最短的方法来删除数据库中的 2 个条目。

此条目与其中第三个表(在以下代码中标记为“c”)有关,此表是引用表,不应删除。

我试过这样做,但失败了。我是否有一个简单的语法错误,或者在 codeigniter 中是不可能的,或者我是否以其他方式这样做?

$this->db->where(" (a.refid='".$this->session->ud('id')."' and b.refid='".$this->session->ud('id')."') and c.type='".$tempid."' and c.templateid=a.templateid and c.slug=b.slug");
  $this->db->delete('a.*,b.*');
  $this->db->from('table_a a, table_b b, table_c c');
  $delinfo=$this->db->affected_rows();

我想删除:
table a - 具有“refid”并通过“type”与 table c 联系的行
表 b - 具有“refid”并通过“slug”与表 c 联系的行
表 c - 没有可删除的内容

【问题讨论】:

  • 很难理解,但如果你在 database.php 中打开 db_debug,你可能会看到 sql 挂起的位置
  • 标识符名称 'a,b FROM table_a a, table_b b, table_c' 太长

标签: php codeigniter


【解决方案1】:

您需要的是删除与连接的结合 - Codeigniters QueryBuilder 不支持带有连接的删除语句 - 所以您必须自己编写

类似下面的东西应该可以工作

$this->db->query('
    DELETE a,b,c FROM table_a a
    JOIN table_c c ON (a.templatedid = c.templateid)
    JOIN table_b b ON (c.slug = b.slug)
    WHERE a.refid = ? AND c.type = ? AND b.refid = ?', 
    array($this->session->ud('id'), $tempid, $this->session->ud('id'))
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    相关资源
    最近更新 更多