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

标签 php mysql codeigniter query-builder

我正在使用 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

关于php - Codeigniter 查询生成器具有带有 IN 语句的子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39557613/

相关文章:

mysql - 在codeigniter中将form_dropdown数据插入mysql数据库

php - 左连接同一字段选择所有行

mysql - Mule - 阻止所有流在单流中的 MySQL 连接失败时关闭

php - 如何从 mysql 隐藏 HTML 中的某些选项

python - Django 操作错误 1405 在syncdb上

c# - 从 DataGridView 自动更新数据库中的值

php - Codeigniter获取mysql值

php - 如何使用 Codeigniter 上传文本文件并将其数据导入到 sql 中?

php - 在php中修改json数组的日期格式

php - SSL3_GET_SERVER_CERTIFICATE :certificate verify failed using xampp on mac