Codeigniter使用联接表删除数据 [英] Codeigniter deleting data with joins tables

查看:122
本文介绍了Codeigniter使用联接表删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在逻辑上,我们可以在SQL中使用JOINS从表中删除数据,例如

Logically in SQL we can delete data from tables with JOINS e.g.

DELETE  clb_subscriber_group_mapping .* FROM clb_subscriber_group_mapping 
INNER JOIN clb_driver_group ON (clb_driver_group.id = clb_subscriber_group_mapping.driver_group_id)
INNER JOIN clb_company ON (clb_company.id = clb_driver_group.company_id)
WHERE clb_company.id = 256 AND clb_subscriber_group_mapping.subscriber_id = 1784;

与上述查询等效的CodeIgniter是什么?

What will be the CodeIgniter equivalent of above query?

CodeIgniter是否支持删除带有联接的查询?

Does CodeIgniter support Delete query with joins?

推荐答案

使用CodeIgniter的Active Record类无法做到这一点.它不支持删除查询中的联接.您必须使用Robin Castlin提到的$this->db->query()执行查询.

You can't do that with CodeIgniter's Active Record class. It doesn't support joins in delete query. You'll have to execute the query using $this->db->query() as mentioned by Robin Castlin.

以下代码摘自核心文件.它是生成DELETE查询的内部组件之一.

The below code is taken from the core files. It is one of the inner components, that generates the DELETE query.

function _delete($table, $where = array(), $like = array(), $limit = FALSE)
{
    $conditions = '';

    if (count($where) > 0 OR count($like) > 0)
    {
        $conditions = "\nWHERE ";
        $conditions .= implode("\n", $this->ar_where);

        if (count($where) > 0 && count($like) > 0)
        {
            $conditions .= " AND ";
        }
        $conditions .= implode("\n", $like);
    }

    $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;

    return "DELETE FROM ".$table.$conditions.$limit;
}

如您所见,其中没有任何内容指定JOIN子句的插入.

As you can see, there's nothing in there that specifies the insertion of a JOIN clause.

这篇关于Codeigniter使用联接表删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆