dongyi6183 2018-03-23 07:15
浏览 493
已采纳

我们如何使用or_where_in以及codeigniter中的其他where条件

Here am stuck with the or_where_in condition.while using this condition the result is obtaining correctly here is my code please have a look

public function get_date_wise_sales_info($start_date, $end_date,$type=NULL,$user_id=NULL,$get_all_user_ids=NULL)
{
    if(!empty($type))
    {
        $this->db->where('type',$type);
    }
    if(!empty($user_id))
    {
        $this->db->where('user_id', $user_id);
    }
    if(!empty($get_all_user_ids))
    {
        $this->db->or_where_in('user_id',$get_all_user_ids);
    }

    $this->db->where("DATE(bills.created_at) BETWEEN '{$start_date}' AND '{$end_date}'");

    $this->db->order_by('bills.id','desc');
    $query = $this->db->get('bills');
    return $query->result();
}

here by using or_where_in condition the results of not selected dates are coming and if i remove or_where_in the result is obtaining correctly.how to write the correct syntax of using or_where_in.i wrote where_in instead of or_where_in but nothing is getting displayed and am using codeigniter 3.1.5 version

  • 写回答

1条回答 默认 最新

  • dqst96444 2018-03-23 09:29
    关注

    I don't know if it's what you are trying to do, but because of the OR statement you may need to use query grouping.

    public function get_date_wise_sales_info($start_date, $end_date,$type=NULL,$user_id=NULL,$get_all_user_ids=NULL)
    {
        if(!empty($type))
        {
            $this->db->where('type',$type);
        }
    
        if(!empty($user_id) && !empty($get_all_user_ids))
        {
            // Check for both, with OR
            $this->db->group_start();
            $this->db->where('user_id', $user_id);
            $this->db->or_where_in('user_id',$get_all_user_ids);
            $this->db->group_end();
        } 
        else if(!empty($user_id))
        {
            // Only check for $user_id
            $this->db->where('user_id', $user_id);
        }
        else if(!empty($get_all_user_ids))
        {
            // Only check for $get_all_user_ids
            $this->db->where_in('user_id', $get_all_user_ids);
        }
    
        $this->db->where("DATE(bills.created_at) BETWEEN '{$start_date}' AND '{$end_date}'");
    
        $this->db->order_by('bills.id','desc');
        $query = $this->db->get('bills');
        return $query->result();
    }
    

    EDIT: this is a bit more complex than simply adding grouping. Updated to check for all possibilities.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记