<i id='kBIHw'><tr id='kBIHw'><dt id='kBIHw'><q id='kBIHw'><span id='kBIHw'><b id='kBIHw'><form id='kBIHw'><ins id='kBIHw'></ins><ul id='kBIHw'></ul><sub id='kBIHw'></sub></form><legend id='kBIHw'></legend><bdo id='kBIHw'><pre id='kBIHw'><center id='kBIHw'></center></pre></bdo></b><th id='kBIHw'></th></span></q></dt></tr></i><div id='kBIHw'><tfoot id='kBIHw'></tfoot><dl id='kBIHw'><fieldset id='kBIHw'></fieldset></dl></div>

    • <bdo id='kBIHw'></bdo><ul id='kBIHw'></ul>
      <tfoot id='kBIHw'></tfoot>
    1. <small id='kBIHw'></small><noframes id='kBIHw'>

    2. <legend id='kBIHw'><style id='kBIHw'><dir id='kBIHw'><q id='kBIHw'></q></dir></style></legend>

      Zend_Db 的复杂 WHERE 子句使用多个 AND OR 运算符

      时间:2023-10-02

              <tbody id='AigeN'></tbody>
            <i id='AigeN'><tr id='AigeN'><dt id='AigeN'><q id='AigeN'><span id='AigeN'><b id='AigeN'><form id='AigeN'><ins id='AigeN'></ins><ul id='AigeN'></ul><sub id='AigeN'></sub></form><legend id='AigeN'></legend><bdo id='AigeN'><pre id='AigeN'><center id='AigeN'></center></pre></bdo></b><th id='AigeN'></th></span></q></dt></tr></i><div id='AigeN'><tfoot id='AigeN'></tfoot><dl id='AigeN'><fieldset id='AigeN'></fieldset></dl></div>
            • <bdo id='AigeN'></bdo><ul id='AigeN'></ul>
            • <tfoot id='AigeN'></tfoot>
                <legend id='AigeN'><style id='AigeN'><dir id='AigeN'><q id='AigeN'></q></dir></style></legend>

              1. <small id='AigeN'></small><noframes id='AigeN'>

                本文介绍了Zend_Db 的复杂 WHERE 子句使用多个 AND OR 运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我想在 Zend_Db 中生成这个复杂的 WHERE 子句:

                I want to generate this complex WHERE clause in Zend_Db:

                SELECT * 
                FROM 'products' 
                WHERE 
                    status = 'active' 
                    AND 
                    (
                        attribute = 'one' 
                        OR 
                        attribute = 'two' 
                        OR 
                        [...]
                    )
                ;
                

                我已经试过了:

                $select->from('product');
                $select->where('status = ?', $status);
                $select->where('attribute = ?', $a1);
                $select->orWhere('attribute = ?', $a2);
                

                然后产生:

                SELECT `product`.* 
                FROM `product` 
                WHERE 
                    (status = 'active') 
                    AND 
                    (attribute = 'one') 
                    OR 
                    (attribute = 'two')
                ;
                

                我确实想出了一种使这项工作起作用的方法,但我觉得通过先使用 PHP 组合OR"子句然后使用 Zend_Db where() 子句组合它们有点作弊".PHP代码:

                I did figure out one method of making this work but I felt it was sort of 'cheating' by using PHP to combine the "OR" clauses first and then combine them using Zend_Db where() clause. PHP code:

                $WHERE = array();
                foreach($attributes as $a):
                    #WHERE[] = "attribute = '" . $a . "'";
                endforeach;
                $WHERE = implode(' OR ', $WHERE);
                
                $select->from('product');
                $select->where('status = ?', $status);
                $select->where($WHERE);
                

                这产生了我正在寻找的东西.但是我很好奇是否有一种官方"的方式来使用 Zend_Db 工具来获得复杂的 WHERE 语句(这真的不是太复杂,只是添加了一些括号),而不是先在 PHP 中组合它.

                That produced what I was looking for. But I'm curious if there's an "official" way of getting that complex WHERE statement (which really isn't too complex, just adding some parenthesis) with using the Zend_Db tool, instead of combining it in PHP first.

                干杯!

                推荐答案

                这将是获得指定括号的官方"方式(参见 Zend_Db_Select 文档):

                This would be the 'official' way to get you the parentheses as specified (see Example #20 in the Zend_Db_Select documentation):

                $a1 = 'one';
                $a2 = 'two';
                $select->from('product');
                $select->where('status = ?', $status);
                $select->where("attribute = $a1 OR attribute = $a2");
                

                因此,鉴于您事先不知道自己有多少属性,您所做的似乎是合理的.

                So, what you are doing does seem reasonable, given that you do not know how many attributes you have ahead of time.

                这篇关于Zend_Db 的复杂 WHERE 子句使用多个 AND OR 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用 Zend Framework 生成 _with_ utf-8 多字节字符 下一篇:格式化 MySQL 查询的结果,就好像它是从控制台运

                相关文章

                最新文章

                  <bdo id='5gdVC'></bdo><ul id='5gdVC'></ul>

                  <legend id='5gdVC'><style id='5gdVC'><dir id='5gdVC'><q id='5gdVC'></q></dir></style></legend>

                  <tfoot id='5gdVC'></tfoot>

                  <small id='5gdVC'></small><noframes id='5gdVC'>

                  1. <i id='5gdVC'><tr id='5gdVC'><dt id='5gdVC'><q id='5gdVC'><span id='5gdVC'><b id='5gdVC'><form id='5gdVC'><ins id='5gdVC'></ins><ul id='5gdVC'></ul><sub id='5gdVC'></sub></form><legend id='5gdVC'></legend><bdo id='5gdVC'><pre id='5gdVC'><center id='5gdVC'></center></pre></bdo></b><th id='5gdVC'></th></span></q></dt></tr></i><div id='5gdVC'><tfoot id='5gdVC'></tfoot><dl id='5gdVC'><fieldset id='5gdVC'></fieldset></dl></div>