Php 限制或_where到codeigniter中2个where之间的特定where
我有以下代码:Php 限制或_where到codeigniter中2个where之间的特定where,php,mysql,codeigniter,where,Php,Mysql,Codeigniter,Where,我有以下代码: $this->db->where('status',1); $this->db->where('category',$category_id); $this->db->or_where('parent_category',$category_id); $this->db->or_where('grandParent_category',$category_id); $this->db-&
$this->db->where('status',1);
$this->db->where('category',$category_id);
$this->db->or_where('parent_category',$category_id);
$this->db->or_where('grandParent_category',$category_id);
$this->db->or_where('parentOfGP_category',$category_id);
$query = $this->db->get('posts');
问题是第一个where被覆盖,它也会得到那些状态为0和2的where,因为下一个or_wheres,我希望or_wheres仅用于where('category',$category_id);而不是地位
在codeigniter中是否有这样做的方法?您可以直接传入查询,而不是使用活动记录,如下所示:
$query = $this->db->query('SELECT name, title, email FROM my_table');
此外,如果您想确定上述活动记录查询的具体形式,请使用以下方法:
echo ($this->db->last_query());
这有点复杂,因为您需要连接
和以及或条件。请看这里:
因此,对于您的情况,应该是这样的:
$this->db->select('*')->from('posts')
->group_start()
->where('category', $category_id)
->or_group_start()
->where('parent_category', $category_id)
->group_end()
->or_group_start()
->where('grandParent_category', $category_id);
->group_end()
->or_group_start()
->where('parentOfGP_category', $category_id);
->group_end()
->group_end()
->where('status', 1)
->get();
此代码未经测试。你可能需要调整一些东西