Php 有没有办法使用逻辑从查询中删除where子句?
我正在使用CodeIgniter。我正在从控制器获取数据 模型代码 我的问题是,查询中有两个where子句,如果发现$select\u filter\u status和$select\u filter\u ACTION为空,我必须删除第二个where条件 有没有办法删除where子句或者我必须使用Php 有没有办法使用逻辑从查询中删除where子句?,php,codeigniter-3,Php,Codeigniter 3,我正在使用CodeIgniter。我正在从控制器获取数据 模型代码 我的问题是,查询中有两个where子句,如果发现$select\u filter\u status和$select\u filter\u ACTION为空,我必须删除第二个where条件 有没有办法删除where子句或者我必须使用 if(){ //with two where clause } else{ //with one where clasue } 您必须在主查询中传递where_2查询。。在$com_报告查询中包含变
if(){
//with two where clause
}
else{
//with one where clasue
}
您必须在主查询中传递where_2查询。。在$com_报告查询中包含变量$where_2
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;
}
现在
现在将其传递给查询生成器
$this->db->where($com_report);
我希望它对您有用是的,您可以在Codeigniter中添加基于条件的where子句,如下所示
$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();
你可以简单地把一个if条件放在这里。这对你有用
if (!empty($where_2){
$this->db->where($where_2);
}
如果$where_2不为空,它将执行第二个where条件,如果为空,它将忽略第二个where
或者,您可以像这样连接查询字符串,以便只使用一个查询子句
$com_report = $com_report . " " . $where_2;
现在不需要第二个where子句
希望它对你有用给我一些时间检查一下你的答案对我也有用。我这边的投票。你能帮我一下这个链接吗?因为格式错误,我认为我不能这样使用。给我一些时间检查给我一些时间检查好的:顺便说一句,你可以通过$q=$this->db->last\u query检查你的查询;并打印r$q;出口把它打印在屏幕上,看看这个查询有什么问题:是的,你的答案对我有用。谢谢你的帮助。
if (!empty($where_2){
$this->db->where($where_2);
}
$com_report = $com_report . " " . $where_2;