CodeIgniter:如何编写 where_in OR where_in 查询?
CodeIgniter : how to write where_in OR where_in query?
我正在使用 codeigniter 查询数据库,我需要在 where 子句中有条件地比较数组。
所以基本上我想做的是:where_in 或 where_in 取决于哪个数组中有数据。所以我要么有一个 user_id 的数组,要么有一个 area_id 的数组。
我不确定如何组合 where_in 比较。这是我需要修改的查询:
public function get_checks_test($org_id,$users,$areas) {
// get todays date
$todays_date = date("Y-m-d");
// build where array
$check_array = array('c.org_id =' => $org_id, 'c.status !=' => '0', 'c.due <=' => $todays_date);
// build query
$this->db->select('c.*,a.check_area_id,b.check_user_id,d.area_name,u.first_name,u.last_name');
$this->db->order_by('c.due', 'asc');
$this->db->where($check_array);
$this->db->where_in('user_id', $users);
$this->db->or_where_in('area_id', $areas);
$this->db->join('check_assigned_areas a','a.check_id = c.check_id','left');
$this->db->join('check_assigned_users b','b.check_id = c.check_id','left');
$this->db->join('areas d','d.area_id = a.check_area_id','left');
$this->db->join('users u','u.id = b.check_user_id','left');
$check_object = $this->db->get('checks c');
return $check_object->result();
}
我认为没有我能找到的 or_where_in 函数。
我可以这样做还是需要重新考虑?
谢谢
我不确定这是不是你想要的:
if(!empty($users)){
$this->db->where_in('user_id', $users);
}
elseif(!empty($areas)){
$this->db->where_in('area_id', $areas);
}
是的,Codeigniter 中有一个 or_where_in 函数。
例子
$arr_price=array(20,40,80,30);
$this->db->or_where_in('price', $arr_price);
//SQL Query: OR price IN(20,40,80,30);
语法:
or_where_in([$key = NULL[, $values = NULL[, $escape = NULL]]])
参数:
$key (string) – The field to search
$values (array) – The values searched on
$escape (bool) – Whether to escape values and identifiers
Returns:
DB_query_builder instance
Return 类型:
object
生成一个 WHERE 字段 IN(‘item’, ‘item’) SQL 查询,如果合适的话用‘OR’连接。
生成一个 WHERE 字段 IN(‘item’, ‘item’) SQL 查询,如果合适的话加入‘OR’。
如果需要更多帮助,我很乐意提供帮助。快乐编码。
当您使用 "left" join 时,在任何情况下都不要将 "where condition" 用作 or_where_in
和 where_in
因为它会为左查询创建 array() 如果没有找到。
$this->db->join('users u', 'u.id = b.check_user_id and u.id=' . $users, 'left');
我正在使用 codeigniter 查询数据库,我需要在 where 子句中有条件地比较数组。
所以基本上我想做的是:where_in 或 where_in 取决于哪个数组中有数据。所以我要么有一个 user_id 的数组,要么有一个 area_id 的数组。
我不确定如何组合 where_in 比较。这是我需要修改的查询:
public function get_checks_test($org_id,$users,$areas) {
// get todays date
$todays_date = date("Y-m-d");
// build where array
$check_array = array('c.org_id =' => $org_id, 'c.status !=' => '0', 'c.due <=' => $todays_date);
// build query
$this->db->select('c.*,a.check_area_id,b.check_user_id,d.area_name,u.first_name,u.last_name');
$this->db->order_by('c.due', 'asc');
$this->db->where($check_array);
$this->db->where_in('user_id', $users);
$this->db->or_where_in('area_id', $areas);
$this->db->join('check_assigned_areas a','a.check_id = c.check_id','left');
$this->db->join('check_assigned_users b','b.check_id = c.check_id','left');
$this->db->join('areas d','d.area_id = a.check_area_id','left');
$this->db->join('users u','u.id = b.check_user_id','left');
$check_object = $this->db->get('checks c');
return $check_object->result();
}
我认为没有我能找到的 or_where_in 函数。
我可以这样做还是需要重新考虑?
谢谢
我不确定这是不是你想要的:
if(!empty($users)){
$this->db->where_in('user_id', $users);
}
elseif(!empty($areas)){
$this->db->where_in('area_id', $areas);
}
是的,Codeigniter 中有一个 or_where_in 函数。
例子
$arr_price=array(20,40,80,30);
$this->db->or_where_in('price', $arr_price);
//SQL Query: OR price IN(20,40,80,30);
语法:
or_where_in([$key = NULL[, $values = NULL[, $escape = NULL]]])
参数:
$key (string) – The field to search
$values (array) – The values searched on
$escape (bool) – Whether to escape values and identifiers
Returns:
DB_query_builder instance
Return 类型:
object
生成一个 WHERE 字段 IN(‘item’, ‘item’) SQL 查询,如果合适的话用‘OR’连接。 生成一个 WHERE 字段 IN(‘item’, ‘item’) SQL 查询,如果合适的话加入‘OR’。
如果需要更多帮助,我很乐意提供帮助。快乐编码。
当您使用 "left" join 时,在任何情况下都不要将 "where condition" 用作 or_where_in
和 where_in
因为它会为左查询创建 array() 如果没有找到。
$this->db->join('users u', 'u.id = b.check_user_id and u.id=' . $users, 'left');