doushi1847 2012-08-14 21:35
浏览 47
已采纳

向Zend_Db_Select添加复杂的WHERE条件[重复]

Possible Duplicate:
Zend Framework Complex Where Statement

How do I add something like this to a Zend_Db_Select while still using quoting?

SELECT x FROM y WHERE (a=1 OR b=2) AND (c=3 OR d=4)

I have seen where() and orWhere(), but I see no way of grouping them with brackets. I realize that I could just write this:

->where('(a=1 OR b=2) AND (c=3 OR d=4)')

But ideally I would like to have 4 separate calls to where() to be able to use the second parameter for quoting (assuming the values 1, 2, 3 etc. are user generated input).

Is there any way to do this in the pure fluent style?

  • 写回答

1条回答 默认 最新

  • dongzhijing8202 2012-08-14 21:39
    关注

    Is there any way to do this in the pure fluent style?

    Short answer: no.

    As you mention, you can use orWhere to write conditions like (a = ?) OR (b = ?).

    Zend_Db_Select automatically puts parentheses around each expression you specify using the where() or orWhere() methods. This helps to ensure that Boolean operator precedence does not cause unexpected results.

    However it is not possible to nest AND and OR conditions in the way you wish.

    If you wrote this:

    ->where('a=?', $a)
    ->orWhere('b=?', $b)
    ->where('c=?', $c)
    ->orWhere('d=?', $d)
    

    It would result in SQL similar to this:

    (a=1) OR (b=2) AND (c=3) OR (d=4)
    

    Which would be interpreted as follows (not what you want):

    (a=1) OR ((b=2) AND (c=3)) OR (d=4)
    

    Alternatives:

    • Write the SQL by hand.
    • Use quoteInto as demonstrated here.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题