问题描述
这是我的问题:
解决方案是对OR语句进行分组,但是我使用CodeIgniters Active Record。有没有办法使用Active Record对OR语句进行分组?
The solution is to group the OR statements, but i'm using CodeIgniters Active Record. Is there a way to group OR statements using Active Record? Or do I have to write the query myself?
我使用 $ this-> db-> where()
与 $ this-> db-> or_where()
这:
WHERE title ='foobar'AND category = 2 OR category = 3
但我需要的是:
WHERE title ='foobar'AND(category = 3)
我无法做到:
$this->db->where("title = 'foobar' AND (category = 2 OR category = 3)");
因为我使用foreach循环添加ORs
because i'm adding ORs using a foreach loop
推荐答案
您可以按照:
$ where =name ='Joe'AND status ='boss'OR status ='active';
$where = "name='Joe' AND status='boss' OR status='active'";
$ this-> db- > where($ where);
$this->db->where($where);
至于您的问题:
$this->db->where("category = 1 AND (category = 2 OR category = 3)");
在.
或直接尝试:
$categories = array(2, 3);
array_walk($categories, function(&$cat) { $cat = 'category = ' . $cat; });
$catstring = implode(" OR ", $categories);
$where = "category = 1 AND ($catstring)";
// => category = 1 AND (category = 2 OR category = 3)
$this->db->where($where);
这篇关于CodeIgniter活动记录 - 组OR语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!