【问题标题】:Codeigniter "where" and "or_where"Codeigniter "where" 和 "or_where"
【发布时间】:2023-03-08 11:49:01
【问题描述】:

我正在尝试使用 codeigniter 的活动记录执行以下 SQL 查询:

SELECT * FROM USERS_OPTIONS WHERE user_id = 3 AND ( option_id = 2 OR option_id = 5 OR option_id = 108 OR .... )

为此,我在 codeigniter 中有这段代码,但它不起作用:

$this -> db -> where("user_id", $user_id);

foreach( $options as $option ){
    $this -> db -> or_where("option_id" , $option["id"]  );
}

$options 是一个数组,用于存储我需要的所有 options_id。

如果我分别尝试这 2 个查询中的任何一个,它们都会起作用。但我无法混合这两个条件。

我怎样才能得到这个?提前致谢。

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    你可以使用$this->db->where_in()

    生成一个 WHERE 字段 IN ('item', 'item') SQL 查询,使用 AND 连接 如果合适

    $names = array('Frank', 'Todd', 'James');
    $this->db->where_in('username', $names);
    // Produces: AND username IN ('Frank', 'Todd', 'James')
    

    在您的情况下,您将拥有:

    $this->db->where_in('option_id', $options)
             ->get_where("your_table", array("user_id" => $user_id))->result();
    

    文档:https://ellislab.com/codeigniter/user-guide/database/active_record.html

    【讨论】:

      【解决方案2】:
      $this->db->select()->from('USERS_OPTIONS')->where("user_id $user_id AND (
      
      // LOOP THROUGH PHP TO PRINT SOME THING LIKE or_where
      
      ));
      

      【讨论】:

        【解决方案3】:

        请检查以下代码,您不能使用活动记录方法连接 AND OR 条件。

        //Filtering by user
        $where_condition = "(user_id = '".$user_id."')";
        
        //Filtering by options
        if(count($options)){
        
            $where_condition .= " AND (";
        
            foreach( $options as $option ){
                $where_condition .= "option_id = '".$option["id"]."' OR ";
            }
        
            $where_condition = substr($where_condition, 0, -3); //Remove last 3 characters OR with space
        
            $where_condition = $where_condition. ")";
        
        }
        
        //Select data                   
        $this->db->select('*'); 
        $this->db->from("TABLE_NAME");
        $this->db->where($where_condition); 
        

        【讨论】:

          【解决方案4】:

          你应该试试$this->db->where_in();。首先收集所有选项 ID,然后像这样使用它:

          生成一个 WHERE 字段 IN ('item', 'item') SQL 查询,如果合适,用 AND 连接

          $names = array('Frank', 'Todd', 'James');
          $this->db->where_in('username', $names);
          // Produces: AND username IN ('Frank', 'Todd', 'James')
          

          【讨论】:

            【解决方案5】:

            最后,这是我发现使用活动记录实现这一目标的最佳方式:

            使用如下数组:

            $options = array ( option_id1 , option_id2, ... , option_idn );
            
            $this -> db -> where("id_user", $id_user);
            $this -> db -> where_in("id_option" , $options  );
            

            这正是我想要的,而且很清楚

            【讨论】:

              猜你喜欢
              • 2013-05-06
              • 2012-04-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-10-20
              • 2019-01-14
              • 2015-05-15
              相关资源
              最近更新 更多