嵌套where子句codeigniter mysql查询 [英] Nested where clauses codeigniter mysql query

查看:320
本文介绍了嵌套where子句codeigniter mysql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以嵌套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 no where() method usage variant with arrays that would allow you to do that. In these situations i usually just build the part in one long string like this:

$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) or escape_str or escape_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() and group_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆