【问题标题】:Codeigniter Query Builder having clause with IN statementCodeigniter 查询生成器具有带有 IN 语句的子句
【发布时间】:2016-09-18 12:24:07
【问题描述】:

我正在使用 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) 周围的引号?

【问题讨论】:

  • HAVING interests IN '(7)' -- 这不是错误。该查询是针对 ID 的 列表,这是您在代码中指定的。您实际看到了什么“错误”(或意外行为)?
  • 问题是'(7)' 周围的引号。使用 PaulD 的答案和 FALSE 解决了这个问题。

标签: php mysql codeigniter query-builder


【解决方案1】:

您可以禁用转义。

如果您使用 CodeIgniter 对其进行转义查询的数据库,您 可以通过传递可选的第三个参数来防止转义内容, 并将其设置为 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 2019-02-22
    相关资源
    最近更新 更多