CodeIgniter: How to use WHERE clause and OR clause

前端 未结 6 976
误落风尘
误落风尘 2021-01-03 19:23

I am using the following code to select from a MySQL database with a Code Igniter webapp:

$query = $this->db->get_where(\'mytable\',array(\'id\'=>10         


        
相关标签:
6条回答
  • 2021-01-03 19:56

    You can use this :

    $this->db->select('*');
    $this->db->from('mytable');
    $this->db->where(name,'Joe');
    $bind = array('boss', 'active');
    $this->db->where_in('status', $bind);
    
    0 讨论(0)
  • 2021-01-03 20:01
    $where = "name='Joe' AND status='boss' OR status='active'";
    
    $this->db->where($where);
    
    0 讨论(0)
  • 2021-01-03 20:11

    Active record method or_where is to be used:

    $this->db->select("*")
    ->from("table_name")
    ->where("first", $first)
    ->or_where("second", $second);
    
    0 讨论(0)
  • 2021-01-03 20:12

    What worked for me :

      $where = '';
       /* $this->db->like('ust.title',$query_data['search'])
            ->or_like('usr.f_name',$query_data['search'])
            ->or_like('usr.l_name',$query_data['search']);*/
            $where .= "(ust.title like '%".$query_data['search']."%'";
            $where .= " or usr.f_name like '%".$query_data['search']."%'";
            $where .= "or usr.l_name like '%".$query_data['search']."%')";
            $this->db->where($where);
    
    
    
    $datas = $this->db->join(TBL_USERS.' AS usr','ust.user_id=usr.id')
                ->where_in('ust.id', $blog_list) 
                ->select('ust.*,usr.f_name as f_name,usr.email as email,usr.avatar as avatar, usr.sex as sex')
                ->get_where(TBL_GURU_BLOG.' AS ust',[
                    'ust.deleted_at'     =>  NULL,
                    'ust.status'     =>  1,
                ]); 
    

    I have to do this to create a query like this :

    SELECT `ust`.*, `usr`.`f_name` as `f_name`, `usr`.`email` as `email`, `usr`.`avatar` as `avatar`, `usr`.`sex` as `sex` FROM `blog` AS `ust` JOIN `users` AS `usr` ON `ust`.`user_id`=`usr`.`id` WHERE (`ust`.`title` LIKE '%mer%' ESCAPE '!' OR  `usr`.`f_name` LIKE '%lok%' ESCAPE '!' OR  `usr`.`l_name` LIKE '%mer%' ESCAPE '!') AND `ust`.`id` IN('36', '37', '38') AND `ust`.`deleted_at` IS NULL AND `ust`.`status` = 1 ;
    
    0 讨论(0)
  • 2021-01-03 20:13
    $where = "name='Joe' AND status='boss' OR status='active'";
    
    $this->db->where($where);
    

    Though I am 3/4 of a month late, you still execute the following after your where clauses are defined... $this->db->get("tbl_name");

    0 讨论(0)
  • 2021-01-03 20:15

    You can use or_where() for that - example from the CI docs:

    $this->db->where('name !=', $name);
    
    $this->db->or_where('id >', $id); 
    
    // Produces: WHERE name != 'Joe' OR id > 50
    
    0 讨论(0)
提交回复
热议问题