如何使用 CodeIgniter 的 ActiveRecord 选择列值不为 NULL 的行?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
活动记录肯定有一些怪癖。当您将数组传递给
$this->db->where()
函数时,它将生成 IS NULL。例如:产生
奇怪的是,没有与负
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:produces
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:produces
CodeIgniter 3
仅限
:生成的查询为:
反向:
生成的查询是:
CodeIgniter 3
Only:
The generated query is:
Inverse:
The generated query is:
Null 不得设置为字符串...
当 null 未包含在引号中时它可以正常工作。
Null must not be set to string...
It works properly when null is not wrapped into quotes.
更好地使用以下内容:
For is not null:
For is null:
update
我们可以在检查 NULL 或时不需要 CodeIgniter 的 Active Record 中不需要第二个参数的情况下使用不为空。正确的用法如下:
For IS NOT NULL:
For IS NULL:
在这两种情况下,您都不需要提供第二个参数。 where 方法自动正确处理 NULL 比较,无需显式第二个参数。
Much better to use following:
For is not null:
For is 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:
For IS NULL:
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.
为了给您提供另一个选择,您可以使用
NOT ISNULL(archived)
作为 WHERE 过滤器。And just to give you yet another option, you can use
NOT ISNULL(archived)
as your WHERE filter.Codeigniter 通过不带任何参数的调用来生成“IS NULL”查询:
生成的查询是:
Codeigniter generates an "IS NULL" query by just leaving the call with no parameters:
The generated query is:
,你可以这样做(如果你想测试 NULL)
如果你想测试 NOT NULL
You can do (if you want to test NULL)
If you want to test NOT NULL
$this->db->or_where('end_date IS', 'NULL', false);
$this->db->or_where('end_date IS', 'NULL', false);
如果您在模型中使用 multi where ,例如:
那么我建议使用以下语法,该语法将在计算 multi where 条件时绕过第二个参数。
If you are using multi where in your model like:
Then I would recommend using the following syntax that will bypass the second parameter in calculating the multi where condition.
检查任一列是否为空的一种方法是
在 php 中如果列有数据,则可以表示为 True,否则表示为 False
在where命令中使用多重比较并检查列数据是否不为空
这样做的
完整示例是我如何在 where 子句(Codeignitor)中过滤列。最后一张显示Not NULL压缩
One way to check either column is null or not is
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