Codeigniter查询生成器具有带有IN语句的子句 [英] Codeigniter Query Builder having clause with IN statement

查看:79
本文介绍了Codeigniter查询生成器具有带有IN语句的子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CodeIgniter查询构建器.我想在其中添加hading子句.我的代码如下所示:(我省略了查询的其他部分):

I am using CodeIgniter query builder. I want to add the having clause in there. My code looks as follows: (i omitted the other parts of the query):

$this->db->having($filterarray);

我事先像这样构建了filterarray:

And i build the filterarray beforehand like this:

$filterarray = array();
        if (!empty($filters['interests'])) {
            $interestids = $this->interests_model->getAllIdsByDescription($filters['interests']);
            $filterarray['interests IN'] = $interestids;
        }

我的函数getAllIdsByDescription看起来像这样:

My function getAllIdsByDescription looks like this:

function getAllIdsByDescription($names){
    $res = "(";

    $query = $this->db->where_in('description', $names)
        ->select('interest_id')
        ->get($this->tableName);
    foreach ($query->result() as $interestid) {
        $res .= $interestid->interest_id;
        $res .= ", ";
    }
    $res = substr($res, 0, -2);
    $res .= ")";
    return $res;
}

它将查询转换为以下内容,从而说明为什么出错:

It translates my query to the following, hence why I have an error:

HAVING interests IN '(7)'

如何删除(7)周围的引号?

How do i remove the quotes around the (7) ?

推荐答案

您可以禁用转义.

如果您使用的是CodeIgniter对其进行转义查询的数据库,则您可以通过传递可选的第三个参数来防止转义内容,并将其设置为FALSE.

If you are using a database that CodeIgniter escapes queries for, you can prevent escaping content by passing an optional third argument, and setting it to FALSE.

$this->db->having('user_id',  45);  // Produces: HAVING `user_id` = 45
$this->db->having('user_id',  45, FALSE);  // Produces: HAVING user_id = 45

不确定仅将单个数组作为一个参数传递时如何实现,但是您需要附加的FALSE参数.

Not sure how to implement this when just passing a single array as one argument, but you need the additional FALSE parameter.

来自 http://www.codeigniter.com/user_guide/database/query_builder.html

这篇关于Codeigniter查询生成器具有带有IN语句的子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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