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

我们如何使用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 电脑出问题了,说是usbmom注册表没
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网