简体   繁体   English

CodeIgniter:如何编写 where_in OR where_in 查询?

[英]CodeIgniter : how to write where_in OR where_in query?

I'm using codeigniter to query a database, and I need to conditionally compare arrays in the where clause.我正在使用 codeigniter 查询数据库,我需要有条件地比较 where 子句中的数组。

So basically I want to do: where_in OR where_in depending on what array has data in it.所以基本上我想做: where_in OR where_in 取决于哪个数组中有数据。 So I will have either an array of user_id OR an array of area_id's.所以我将有一个 user_id 数组或一个 area_id 数组。

I'm not sure how to combine the where_in comparison.我不确定如何结合 where_in 比较。 This is the query i need to amends:这是我需要修改的查询:

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();
}

I don't think there is a or_where_in function that I can find.我认为我找不到 or_where_in 函数。

Can I do it like this or do I need a rethink?我可以这样做还是需要重新考虑?

Thanks谢谢

Yes, there is a or_where_in function in Codeigniter.是的, Codeigniter中有一个or_where_in函数。

Example示例

$arr_price=array(20,40,80,30);
$this->db->or_where_in('price', $arr_price);
//SQL Query: OR price IN(20,40,80,30);

Syntax:语法:

or_where_in([$key = NULL[, $values = NULL[, $escape = NULL]]])

Parameters:参数:

$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 type:返回类型:

object

Generates a WHERE field IN('item', 'item') SQL query, joined with 'OR' if appropriate.生成一个 WHERE 字段 IN('item', 'item') SQL 查询,如果合适的话用 'OR' 连接。 Generates a WHERE field IN('item', 'item') SQL query, joined with 'OR' if appropriate.生成一个 WHERE 字段 IN('item', 'item') SQL 查询,如果合适的话用 'OR' 连接。

If more help needed, I'm happy to help.如果需要更多帮助,我很乐意提供帮助。 Happy Coding.快乐编码。

I'm not sure if this is what you want:我不确定这是否是您想要的:

if(!empty($users)){
    $this->db->where_in('user_id', $users);
}
elseif(!empty($areas)){
    $this->db->where_in('area_id', $areas);
}

当您使用“左”连接时,无论如何都不要使用“where条件”作为or_where_inwhere_in因为如果未找到,它将为该左查询创建 array() 。

$this->db->join('users u', 'u.id = b.check_user_id and u.id=' . $users, 'left');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM