Есть ли способ удалить предложение where из запроса, используя логику? - PullRequest
0 голосов
/ 12 февраля 2019

Я использую CodeIgniter.Я получаю данные от контроллера.

Код модели

 $select_filter_status=$parameters['select_filter_status'];
 $select_filter_action= $parameters['select_filter_action'];

if(($select_filter_status=='') && ($select_filter_action=='')){
       $where_2=" "; // I don't have any condition here. I have to display all the records

}
else if(($select_filter_status=='') || ($select_filter_action=='')){

        if ($select_filter_action=='') {
            $where_2="product_order.o_order_status=".$select_filter_status;
      }
      else{
            $where_2="product_order.o_order_status_confirm=".$select_filter_action;
      }
}
else{
        $where_2="product_order.o_order_status=".$select_filter_status." and product_order.o_order_status_confirm=".$select_filter_action;
  }




$this->db->select("*");
$this->db->from('tbl_customer');
$this->db->join('tbl_customer_billing', 'tbl_customer.cust_id=tbl_customer_billing.c_b_id', 'LEFT');
$this->db->join('tbl_customer_shipping', 'tbl_customer.cust_id=tbl_customer_shipping.cust_id', 'LEFT');
$this->db->where($com_report);
$this->db->where($where_2); // I want to remove this where clause when there is empty status and action
$this->db->order_by('product_order.o_date_of_added','DESC');
$query = $this->db->get();
$result = $query->result();

Моя проблема в том, что в запросе есть два условия where, и мне нужно удалитьсекунда where condition, если $select_filter_status и $select_filter_action найдены пустыми.

Есть ли способ удалить пункт where, или я должен использовать что-то вроде

if(){
//with two where clause
}
else{
//with one where clasue
}

Ответы [ 3 ]

0 голосов
/ 12 февраля 2019

Да, вы можете добавить предложение where на основе условия в Codeigniter, например,

$this->db->select("*");
$this->db->from('tbl_customer');
$this->db->join('tbl_customer_billing', 'tbl_customer.cust_id=tbl_customer_billing.c_b_id', 'LEFT');
$this->db->join('tbl_customer_shipping', 'tbl_customer.cust_id=tbl_customer_shipping.cust_id', 'LEFT');
$this->db->where($com_report);
if(!empty($select_filter_status) AND !empty($select_filter_action)){
    $this->db->where($where_2); 
}


$this->db->order_by('product_order.o_date_of_added','DESC');
$query = $this->db->get();
$result = $query->result();
0 голосов
/ 12 февраля 2019

Вы можете просто поставить только один, если условие здесь, как это.Это будет работать для вас.

if (!empty($where_2){
   $this->db->where($where_2);
}

, если $ where_2 не пусто, оно выполнит секунду где условие, если оно пусто, оно проигнорирует второе где.

Или вы можете объединить запросСтрока, подобная этой, чтобы использовать только одно условие запроса

$com_report = $com_report . " " . $where_2;

Теперь нет необходимости во втором предложении where, надеюсь, это будет работать для вас

0 голосов
/ 12 февраля 2019

Вы должны передать запрос where_2 в своем основном запросе .. конкатенировать переменную $ where_2 в запросе $ com_report

if(($select_filter_status=='') && ($select_filter_action=='')){
       $where_2=" "; // I don't have any condition here. I have to display all the records

}
else if(($select_filter_status=='') || ($select_filter_action=='')){

        if ($select_filter_action=='') {
            $where_2="and product_order.o_order_status=".$select_filter_status;
      }
      else{
            $where_2="and product_order.o_order_status_confirm=".$select_filter_action;
      }
}
else{
        $where_2="and product_order.o_order_status=".$select_filter_status." and product_order.o_order_status_confirm=".$select_filter_action;
  }

NOW

$com_report ='your where statements ' .$where_2 ;

передайте его построителю запросов сейчас

$this->db->where($com_report);

я надеюсь, что он будет работать для вас

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...