【问题标题】:How to select rows where column value IS NOT NULL using CodeIgniter's ActiveRecord?如何使用 CodeIgniter 的 ActiveRecord 选择列值不为 NULL 的行?
【发布时间】:2011-01-30 04:10:18
【问题描述】:

我正在使用 CodeIgniter 的 Active Record 类来查询 MySQL 数据库。我需要选择字段未设置为 NULL 的表中的行:

$this->db->where('archived !=', 'NULL');
$q = $this->db->get('projects');

只返回这个查询:

SELECT * FROM projects WHERE archived != 'NULL';

archived 字段是 DATE 字段。

有没有更好的方法来解决这个问题?我知道我可以自己编写查询,但我想在我的代码中坚持使用 Active Record。

【问题讨论】:

    标签: php mysql codeigniter activerecord null


    【解决方案1】:

    如果您在模型中使用 multi where,例如:

    function getCommonRecords($id, $tbl_name, $multi_where='') {
        $this->db->select('*');
        $this->db->where('isDeleted', '0');
        if ($id > 0) {
            $this->db->where('id', $id);
        }
        if ($multi_where != '') {
            foreach ($multi_where as $key => $val) {
                $this->db->where($key, $val);
            }
        }
        $queryResult = $this->db->get($tbl_name);
        return $queryResult->result_array();
    }
    

    那么我建议使用以下语法,在计算多 where 条件时会绕过第二个参数。

     $this->api->getCommonRecords(NULL,'contracts', ['id' =>,'party1Sign IS NOT NULL'=>NULL,'party2Sign IS NOT NULL'=>null]);
    

    【讨论】:

      【解决方案2】:

      更好地使用以下内容:

      For 不为空:

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

      为空:

      where('archived', null);
      

      【讨论】:

        【解决方案3】:

        你可以做(​​如果你想测试NULL)

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

        如果你想测试 NOT NULL

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

        【讨论】:

        • 你在哪里找到这个where_exec()方法?我在我的 CI 项目中的任何地方都没有看到它,而且我在网上找不到一个提到它的名字的资源。请通过文档链接支持此答案。
        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          CodeIgniter 3

          仅:

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

          生成的查询是:

          WHERE archived IS NOT NULL;
          

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

          逆向:

          $this->db->where('archived');
          

          生成的查询是:

          WHERE archived IS NULL;
          

          【讨论】:

            【解决方案6】:

            Codeigniter 只通过不带参数的调用来生成“IS NULL”查询:

            $this->db->where('column');
            

            生成的查询是:

            WHERE `column` IS NULL
            

            【讨论】:

            • OP 正在寻找 IS NOT NULL 而不是 IS NULL
            • 这是另一个问题的正确答案。
            【解决方案7】:

            为了给您另一个选择,您可以使用NOT ISNULL(archived) 作为您的 WHERE 过滤器。

            【讨论】:

              【解决方案8】:

              检查任一列是否为空的一种方法是

              $this->db->where('archived => TRUE);
              $q = $this->db->get('projects');
              

              在php中,如果列有数据,则可以表示为True,否则为False 在 where 命令中使用多重比较并检查列数据是否不为空 像这样做

              这是我如何在 where 子句 (Codeignitor) 中过滤列的完整示例。最后一个显示Not NULL压缩

              $where = array('somebit' => '1', 'status' => 'Published', 'archived ' => TRUE );
              $this->db->where($where);
              

              【讨论】:

              • 我刚试过这个并输出它生成的 SQL:SELECT * FROM (`test`) WHERE `somebit` = '1' AND `status` = 'Published' AND `sc_id` = 1。检查一列是否等于 1 和检查一列是否不为空是截然不同的。
              【解决方案9】:

              Active Record 肯定有一些怪癖。当您将数组传递给$this->db->where() 函数时,它将生成一个 IS NULL。例如:

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

              产生

              WHERE `archived` IS NULL 
              

              怪癖是没有对应的否定IS NOT NULL。但是,有一种方法可以产生正确的结果并仍然逃避语句:

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

              生产

              WHERE `archived` IS NOT NULL
              

              【讨论】:

              • +1 因为where('archived IS NOT NULL') 这仍然保护标识符而接受的答案没有。
              【解决方案10】:

              不得将 Null 设置为字符串...

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

              当 null 不被引号括起来时,它可以正常工作。

              【讨论】:

              • @GusDeCooL 不确定这是否真的有效。使用此输出...“字段不是”,不带 NULL。接受的答案似乎是正确执行此操作的方法。 ellislab.com/forums/viewthread/119444/#593454 - 提供更多信息。
              • -1 因为它不起作用!我尝试了类似的变体: $this->db->where('when_removed is', null);给出了一个数据库错误并显示生成的查询包括:...WHERE "when_removed" is ORDER BY "last_name" asc...
              【解决方案11】:
              where('archived IS NOT NULL', null, false)
              

              【讨论】:

              • 请注意,当您将第三个参数设置为 FALSE 时,CodeIgniter 不会尝试使用反引号保护您的字段或表名。
              • 另外值得一提的是,您可以在传递数组参数时使用它:where(array("foo" => "bar", "archived IS NOT NULL" => null))。非常不直观,但有效。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-11-11
              • 2022-08-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多