【发布时间】:2015-02-08 08:24:35
【问题描述】:
我需要使用 CodeIgniter 运行条件为 where 和 or_where 的查询。代码:
$this->db->select('*');
$this->db->from('registration');
$this->db->where('MemberStatus', 1);
$this->db->where('Gender', $gender);
if(!empty($city))
$this->db->where_in('City', $city);
if(!empty($education_sector))
$this->db->where_in('education_sector', $education_sector);
if(!empty($degree))
$this->db->where_in('Education', $degree);
$this->db->or_where_in('Education1', $degree);
上面的代码创建了一个这样的查询:
SELECT *
FROM (`registration`)
WHERE `MemberStatus` = 1
AND `Gender` = '1'
AND (Age BETWEEN "18" AND "30")
AND `Education` IN ('BCom', 'MCom')
OR `Education1` IN ('BCom', 'MCom')
如果满足or_where 子句,则不遵守其他条件。如何对引用$degree的条件进行分组?
【问题讨论】:
-
我发现您的问题对于所需的确切 sql 含糊不清。请通过表达您需要的确切 sql 来完成此问题。此外,
!empty()的使用似乎也不合适。为什么第一次访问时检查$degree是否存在,但第二次访问变量时却不保证变量存在。这很奇怪……或者是错误的。解决方法是使用group_start()和group_end(),但问题是你的问题太模糊,无法自信地回答。
标签: php codeigniter activerecord grouping multiple-conditions