【问题标题】:Codeigniter combined AND OR condition not working [duplicate]Codeigniter组合AND OR条件不起作用[重复]
【发布时间】:2018-01-08 10:00:02
【问题描述】:

代码点火器:

$this->db->select("item.*");
$this->db->from("items");
$this->db->where_not_in('item.ID', $Not);
$this->db->where_in('item_sub_category.Sub_category', $Sub_category);
$this->db->or_where_in('item_category.Category',$Category, NULL, FALSE);
$this->db->limit(4);
return $this->db->get()->result_array();

输出:

SELECT `item`.* 
FROM   `item` 
       LEFT JOIN `item_category` 
              ON `item_category`.`item` = `item`.`id` 
       LEFT JOIN `item_sub_category` 
              ON `item_sub_category`.`item` = `item`.`id` 
WHERE  `item`.`id` NOT IN( '12' ) 
       AND `item_sub_category`.`sub_category` IN( '65', '66', '67', '68' ) 
        OR `item_category`.`category` IN( '35', '36' ) 
LIMIT  4 

预期输出:

SELECT `item`.* 
FROM   `item` 
       LEFT JOIN `item_category` 
              ON `item_category`.`item` = `item`.`id` 
       LEFT JOIN `item_sub_category` 
              ON `item_sub_category`.`item` = `item`.`id` 
WHERE  `item`.`id` NOT IN( '12' ) 
       AND (`item_sub_category`.`sub_category` IN( '65', '66', '67', '68' ) 
        OR `item_category`.`category` IN( '35', '36' )) 
LIMIT  4

谁能告诉我如何使用活动记录实现我的预期输出?

【问题讨论】:

  • where_inor_where_in 将同时运行两个查询,即使您添加另一个 where_in 它在一个括号内运行。

标签: php mysql codeigniter


【解决方案1】:

您只需在活动记录中添加join。如下所示。

$this->db->select("item.*");
$this->db->from("items");
$this->db->join("item_category","`item_category`.`item` = `item`.`id`","left");
$this->db->join("item_sub_category","`item_sub_category`.`item` = `item`.`id`","left");
$this->db->where_not_in('item.ID', $Not);
$this->db->where_in('item_sub_category.Sub_category', $Sub_category);
$this->db->or_where_in('item_category.Category',$Category, NULL, FALSE);
$this->db->limit(4);
return $this->db->get()->result_array();

【讨论】:

    【解决方案2】:

    // 试试这个..

    $array = array('item_sub_category.Sub_category' => $Sub_category, 'item_category.Category' => $Category);
    
        $this->db->select("item.*");
            $this->db->from("items");
            $this->db->join("item_category","`item_category`.`item` = `item`.`id`","left");
            $this->db->join("item_sub_category","`item_sub_category`.`item` = `item`.`id`","left");
            $this->db->where_not_in('item.ID', $Not);
            $this->db->where_in($array);
            $this->db->limit(4);
            return $this->db->get()->result_array();
    

    【讨论】:

      【解决方案3】:

      您没有输入整个查询 - 但如果它与您有关的 where 条件 - 您可以执行以下操作:

      $this->db
          ->select("item.*")
          ->from("item")
          ->where_not_in('item.ID', $Not)
          ->group_start()
              ->where_in('item_sub_category.Sub_category', $Sub_category)
              ->or_where_in('item_category.Category',$Category, NULL, FALSE)
          ->group_end()
          ->limit(4);
      
      echo $this->db->get_compiled_select();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        • 2012-10-10
        • 2012-06-18
        • 2017-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多