嵌套where子句codeigniter mysql查询

Tho*_*son 3 codeigniter

有没有办法嵌套where子句?例如:

SELECT * FROM table WHERE (colA = 'valueA' AND colB = 'valueB') OR (colA = 'valueC' AND colB = 'valueD')
Run Code Online (Sandbox Code Playgroud)

我知道我可以把它写成query函数调用,例如:

$this->db->query("SELECT ...")
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有一种"正确"的方式在codeigniter中做到这一点,例如:

$this->db->where(array('colA'=>'valueA'), array('colB'=>valueB'))->or_where(array('colA'=>'valueC'), array('colB'=>'valueD'))
Run Code Online (Sandbox Code Playgroud)

谢谢

com*_*857 7

有了codeigniter 3,现在有了,请看更新!
没有where()使用数组的方法用法变体可以让你这样做.在这些情况下,我通常只在一个长字符串中构建部件,如下所示:

$this->db->where("
    (
    (colA = '".$this->db->escape($v0)."' and colB = '".$this->db->escape($v1)."') 
    or 
    (colA = '".$this->db->escape($v2)."' and colB = '".$this->db->escape($v3)."') 
    )
");
Run Code Online (Sandbox Code Playgroud)

逃离是可以做到的escape(做了一些自动检测)或escape_strescape_like_str手动这取决于预期的参数是什么或使用中的谓语.

如果我在使用Datamapper库的项目上,我更喜欢在构建这些类型的查询时使用group_start()group_end()方法,它们有很多不同的风格.

更新

现在,Codeigniter 3 在查询构建器中具有分组方法,因此您可以执行->group_start()s和->group_end()s.