php 值为 NULL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19790778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Value is NULL Codeigniter
提问by JohnSmith
Im trying to create the following statement (which works):
我试图创建以下语句(有效):
SELECT id, COUNT(*) AS item_count FROM message WHERE user_id_to = '1' AND read_date IS NULL GROUP BY message_id
With Codeigniters Active Record. My code looks like this:
使用 Codeigniters Active Record。我的代码如下所示:
$this->db->select('id');
$this->db->from('message');
$this->db->where('user_id_to', $this->session->userdata('id'));
$this->db->where(array('read_date' => NULL));
$this->db->group_by('message_id');
echo $this->db->count_all_results();
I have checked so $this->session->userdata('id') outputs the same ID as my "regular" SQL-statement and it is correct.
我已经检查过 $this->session->userdata('id') 输出与我的“常规” SQL 语句相同的 ID,它是正确的。
The strange thing is that my "regular" statement returns 2, which is right. But my Codeigniter statmenet returns 3, which is obviously wrong.
奇怪的是,我的“常规”语句返回 2,这是正确的。但是我的 Codeigniter statmenet 返回 3,这显然是错误的。
What am I doing wrong?
我究竟做错了什么?
采纳答案by Narf
count_all_results()
will replace your whole SELECT clause and the produced query will be this:
count_all_results()
将替换您的整个 SELECT 子句,生成的查询将是:
SELECT COUNT(*) AS numrows
FROM message
WHERE user_id_to = <your value> AND read_date IS NULL
GROUP BY message_id
... I'm skipping any parenthesis and escape characters of course, but they are irrelevant here.
...我当然会跳过任何括号和转义字符,但它们在这里无关紧要。
Just put the whole thing in your select()
call:
只需将整个事情放在您的select()
电话中:
$this->db->select('id, COUNT(*) as item_count');
回答by Petra
Try this:
尝试这个:
$this->db->where('read_date IS NULL', null, false);
The third parameter tells him not to escape the clause...
第三个参数告诉他不要逃避条款......
回答by JohnSmith
It was the group by in combination with count_all_results(); Found this thread with a solution: http://ellislab.com/forums/viewthread/131724/
这是 group by 与 count_all_results() 组合;找到这个线程的解决方案:http: //ellislab.com/forums/viewthread/131724/
回答by Amir Qayyum Khan
This is the way of multiple comparison in where command. to check if column data is not null do it like
这是where命令中多重比较的方式。检查列数据是否不为空
'sc_id' => TRUE
'sc_id' => 真
here is the complete example how I am filter columns in where clause (Codeignitor). The last one show Not NULLCompression
这是我如何在 where 子句(Codeignitor)中过滤列的完整示例。最后一个显示Not NULLCompression
$where = array('somebit' => '1', 'status' => 'Published', 'sc_id' => TRUE );
$this->db->where($where);
回答by César Eduardo Cárdenas Fuentes
Well, i think another way it could be:
好吧,我认为另一种方式可能是:
$this->db->where('read_date IS NOT', 'NULL');
Haven't tested this yet, but in theory should work, someone please confirm.
尚未对此进行测试,但理论上应该可行,请有人确认。