php Codeigniter WHERE AND OR 在一起

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20866323/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:37:28  来源:igfitidea点击:

Codeigniter WHERE AND OR together

phpdatabasecodeignitercombinationswhere-clause

提问by Wasim A.

I am trying to combine Where And OR in Codeigniter via code.

我正在尝试通过代码在 Codeigniter 中结合 Where 和 OR。

$this->db->where("id",1);
$this->db->where("status","live")->or_where("status","dead");

Appeared result is as query
where id=1 and status='live' OR status='dead'
Whereas i need result for this query.
where id=1 and (status='live' OR status='dead');

出现的结果作为查询
where id=1 and status='live' OR status='dead'
而我需要这个查询的结果。
where id=1 and (status='live' OR status='dead');

NOTE: Please don't give solution of string passed as parameter from where function. That's not fit into my code. I Just simplified my problem above.

注意:请不要给出从 where 函数作为参数传递的字符串的解决方案。这不适合我的代码。我只是简化了上面的问题。

回答by PravinS

you can use where_in() like this

你可以像这样使用 where_in()

$status_array = array('live','dead');
$this->db->where("id",1);
$this->db->where_in('status', $status_array);

回答by Wasim A.

Just found solution, if it can help some one.

刚刚找到解决方案,如果它可以帮助某人。

$this->db->where("id",1)->where("(status='live' OR status='dead')");

回答by rial

you just add ->group_start() and ->group_end()

你只需添加 ->group_start() 和 ->group_end()

$this->db->where("id",1);
$this->db->where("status","live")
->group_start()
->or_where("status","dead")
->group_end();