嵌套where子句codeigniter mysql查询 [英] Nested where clauses codeigniter mysql query
问题描述
有什么办法可以嵌套where子句吗?例如:
SELECT * FROM table WHERE(colA ='valueA'AND colB ='valueB')OR(colA ='valueC 'AND colB ='valueD')
我知道我可以把它写入一个 query
函数调用例如:
$ this-& SELECT ...)
但我想知道是否有一个正确 in codeigniter例如:
$ this-> db->其中(array('colA'=>'valueA' ),array('colB'=> valueB')) - > or_where(array('colA'=>'valueC'),array('colB'=>'valueD'))
感谢
解决方案使用codeigniter 3,现在有了,看到更新!
没有where()
在这些情况下,我通常只是建立一个长字符串中的部分,如下所示:$ this-> db-> where(
(
(colA ='。$ this-> db-> escape($ v0)。 ='。$ this-> db-> escape($ v1)。')
或
(colA ='。$ this-> db-& )。'和colB ='。$ this-> db-> escape($ v3)。'')
)
);
可以使用
escape
一些自动检测)或escape_str
或escape_like_str
手动,取决于预期的参数或使用的谓词。 / p>
如果我在使用 Datamapper库的项目, a>,我喜欢在构建这类查询时使用
group_start()
和group_end()
有这些的很多不同的风味。
更新
现在使用Codeigniter 3,其中在查询构建器中有分组方法,因此您可以执行
- > group_start()
和- > group_end()
。Is there any way to get nested where clauses? e.g.:
SELECT * FROM table WHERE (colA = 'valueA' AND colB = 'valueB') OR (colA = 'valueC' AND colB = 'valueD')
I know I could just write this into a
query
function call e.g.:$this->db->query("SELECT ...")
But I was wondering if there was a "proper" way to do it in codeigniter e.g.:
$this->db->where(array('colA'=>'valueA'), array('colB'=>valueB'))->or_where(array('colA'=>'valueC'), array('colB'=>'valueD'))
thanks
解决方案With codeigniter 3, now there is, see the update!
There's noIn these situations i usually just build the part in one long string like this:where()
method usage variant with arrays that would allow you to do that.$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)."') ) ");
Escaping can be done with
escape
(does some autodetection) orescape_str
orescape_like_str
manually depending on what the parameter expected to be or what the predicate in use.If i'm on a project that uses the Datamapper library, i prefer to use the
group_start()
andgroup_end()
methods when building these kind of queries, they have a lot of different flavor of these.Update
Now with, Codeigniter 3 which have grouping methods in the query builder, so you can do
->group_start()
s and->group_end()
s.这篇关于嵌套where子句codeigniter mysql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!