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