如何使用 CodeIgniter 的 ActiveRecord 选择列值不为 NULL 的行?

发布于 2024-08-26 05:16:25 字数 396 浏览 5 评论 0原文

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

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

这只返回此查询:

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

archived 字段是一个 DATE 字段。

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

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->db->where('archived !=', 'NULL');
$q = $this->db->get('projects');

That only returns this query:

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

The archived field is a DATE field.

Is there a better way to solve this? I know I can just write the query myself, but I want to stick with the Active Record throughout my code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

一抹淡然 2024-09-02 05:16:25
where('archived IS NOT NULL', null, false)
where('archived IS NOT NULL', null, false)
滥情空心 2024-09-02 05:16:25

活动记录肯定有一些怪癖。当您将数组传递给 $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

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
肩上的翅膀 2024-09-02 05:16:25

CodeIgniter 3

仅限

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

:生成的查询为:

WHERE archived IS NOT NULL;

$this->db->where('存档 IS NOT NULL',null,false); <<没有必要

反向:

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

生成的查询是:

WHERE archived IS NULL;

CodeIgniter 3

Only:

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

The generated query is:

WHERE archived IS NOT NULL;

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

Inverse:

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

The generated query is:

WHERE archived IS NULL;
归途 2024-09-02 05:16:25

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

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

当 null 未包含在引号中时它可以正常工作。

Null must not be set to string...

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

It works properly when null is not wrapped into quotes.

德意的啸 2024-09-02 05:16:25

更好地使用以下内容:

For is not null:

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

For is null:

where('archived', null);

update

我们可以在检查 NULL 或时不需要 CodeIgniter 的 Active Record 中不需要第二个参数的情况下使用不为空。正确的用法如下:

For IS NOT NULL:

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

For IS NULL:

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

在这两种情况下,您都不需要提供第二个参数。 where 方法自动正确处理 NULL 比较,无需显式第二个参数。

Much better to use following:

For is not null:

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

For is null:

where('archived', null);

update

We can use without second parameter for where in CodeIgniter's Active Record is not needed when checking for NULL or IS NOT NULL. Here's the correct usage:

For IS NOT NULL:

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

For IS NULL:

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

In both cases, you don't need to provide a second parameter. The where method automatically handles NULL comparisons correctly without the need for an explicit second parameter.

请远离我 2024-09-02 05:16:25

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

And just to give you yet another option, you can use NOT ISNULL(archived) as your WHERE filter.

⊕婉儿 2024-09-02 05:16:25

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

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

生成的查询是:

WHERE `column` IS NULL

Codeigniter generates an "IS NULL" query by just leaving the call with no parameters:

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

The generated query is:

WHERE `column` IS NULL
烂柯人 2024-09-02 05:16:25

,你可以这样做(如果你想测试 NULL)

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

如果你想测试 NOT NULL

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

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) 
往日情怀 2024-09-02 05:16:25

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

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

话少心凉 2024-09-02 05:16:25

如果您在模型中使用 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();
}

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

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

If you are using multi where in your model like:

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();
}

Then I would recommend using the following syntax that will bypass the second parameter in calculating the multi where condition.

 $this->api->getCommonRecords(NULL,'contracts', ['id' =>,'party1Sign IS NOT NULL'=>NULL,'party2Sign IS NOT NULL'=>null]);
淡墨 2024-09-02 05:16:25

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

$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);

One way to check either column is null or not is

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

in php if column has data, it can be represent as True otherwise False
To use multiple comparison in where command and to check if column data is not null
do it like

here is the complete example how I am filter columns in where clause (Codeignitor). The last one show Not NULL Compression

$where = array('somebit' => '1', 'status' => 'Published', 'archived ' => TRUE );
$this->db->where($where);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文