带有 IN 语句的子句的 Codeigniter 查询生成器

Codeigniter Query Builder having clause with IN statement

我正在使用 CodeIgniter 查询生成器。我想在那里添加 having 子句。 我的代码如下所示: (我省略了查询的其他部分):

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

然后我像这样预先构建过滤器数组:

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

我的函数 getAllIdsByDescription 如下所示:

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;
}

它将我的查询翻译成以下内容,这就是我出现错误的原因:

HAVING interests IN '(7)'

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

您可以禁用转义。

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 参数。

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