How to select rows where column value IS NOT NULL using CodeIgniter's ActiveRecord?

前端 未结 10 1800
太阳男子
太阳男子 2020-12-04 17:15

I\'m using CodeIgniter\'s Active Record class to query the MySQL database. I need to select the rows in a table where a field is not set to NULL:

$this->d         


        
相关标签:
10条回答
  • 2020-12-04 18:00

    Much better to use following:

    For is not null:

    where('archived IS NOT NULL', null);
    

    For is null:

    where('archived', null);
    
    0 讨论(0)
  • 2020-12-04 18:04

    $this->db->or_where('end_date IS', 'NULL', false);

    0 讨论(0)
  • 2020-12-04 18:07

    You can do (if you want to test NULL)

    $this->db->where_exec('archived IS NULL) 
    

    If you want to test NOT NULL

    $this->db->where_exec('archived IS NOT NULL) 
    
    0 讨论(0)
  • 2020-12-04 18:13

    The Active Record definitely has some quirks. When you pass an array to the $this->db->where() function it will generate an IS NULL. For example:

    $this->db->where(array('archived' => NULL));
    

    produces

    WHERE `archived` IS NULL 
    

    The quirk is that there is no equivalent for the negative IS NOT NULL. There is, however, a way to do it that produces the correct result and still escapes the statement:

    $this->db->where('archived IS NOT NULL');
    

    produces

    WHERE `archived` IS NOT NULL
    
    0 讨论(0)
提交回复
热议问题