在 Array 的两个相应元素之间使用 AND,在 Codeigniter 活动记录中在它们之间使用 OR


Using AND between two corresponding elements of Array and OR between them in Codeigniter Active Record

请原谅我,如果我很笨,连续编码了 30 个小时

需要最终输出:

cities_extended中选择 *,其中 ( state_code = "MO"和 county = 'GREENE') 或 ( state_code = 'NY' AND county = 'GREENE')

我的尝试:我有这个查询(为测试而编写),我在代码点火器中为它编写了代码,但我未能将其正确转换为代码点火器的活动记录。我知道编码点火器的where_in和or_where_in功能。我尝试了以下代码:

// some code 
$counties = $query->result_array();
for($i = 0; $i < count($counties); $i++)
{
   $this->db->or_where('county', $counties[$i]['county']);
   $this->db->where('state_code', $counties[$i]['state_code']);
}
   $this->db->select('zip');
   $zips_query = $this->db->get('cities_extended');
   return $this->db->last_query();
 // some code

这将给出以下输出:

"SELECT `zip` FROM `cities_extended` WHERE `county` = 'Laclede' AND `state_code` = 'MO' OR `county` = 'Greene' AND `state_code` = 'MO' OR `county` = 'Webster' AND `state_code` = 'MO'"

关键:我如何使用Codeigniter的活动记录放置(县="帮助"state_code="我")(县="将"和state_code="你")。

你可以在下面的代码中这样做

$this -> db -> select('*') -> from ('cities_extended') -> where("(state_code = 'MO' AND county= 'GREENE')") -> or_where("(state_code = 'NY' AND county = 'GREENE')");
$result = $this -> db -> get();

编辑:请做下面的一个

for($i = 0; $i < count($counties); $i++)
{
   $this->db->or_where("'county' = '".$counties[$i]['county']."' AND 'state_code' = '".$counties[$i]['state_code."']");
}
这样做

。我从阿伦的回答中得到了提示。还是要做一点改变。我更改了循环体,如下所示:

for($i = 0; $i < count($counties); $i++)
    $this->db->or_where("( state_code = '".$counties[$i]['state_code']. "' AND county = '".$counties[$i]['county']."' )");
$this->db->select('*');
$this->db->from('cities_extended');
$this->db->where("state_code",  'MO');
$this->db->where('county','GREENE') 
$this->db->or_where("state_code",'NY');
$result = $this->db->get();