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 对于这个问题的算法代码
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据