【问题标题】:Grouping WHERE clauses with Zend_Db_Table_Abstract用 Zend_Db_Table_Abstract 对 WHERE 子句进行分组
【发布时间】:2010-11-13 20:05:31
【问题描述】:

有人知道用 Zend_Db 对 where 子句进行分组的方法吗?基本上我有这个查询

$sql = $table->select()
             ->where('company_id = ?', $company_id)
             ->where('client_email = ?', $client_email)
             ->orWhere('client_email_alt = ?', $client_email);

这是给我的:

SELECT `clients`.* FROM `clients` WHERE (company_id = '1') AND (client_email = 'email@address.com') OR (client_email_alt = 'email@address.com')

但我需要它给我这个,OR 语句被分组的地方:

SELECT `clients`.* FROM `clients` WHERE (company_id = '1') AND ((client_email = 'email@address.com') OR (client_email_alt = 'email@address.com'))

【问题讨论】:

    标签: zend-framework zend-db-table


    【解决方案1】:

    我需要组合 AND/OR 语句,但有条件地包括 OR 语句,仅在某些情况下添加它们。这个解决方案是对我所做的事情的改编,基于对已接受答案的小修改。

    $sql = $table->select()
             ->where('company_id = ?', $company_id);
    
    $orWhereClauses = [];
    // We could add a conditional statement to add this statement
    $orWhereClauses[] = $db->quoteInto('client_email = ?', $email1);
    // Same applies to this statement
    $orWhereClauses[] = $db->quoteInto('client_email_alt = ?', $email2);
    
    $sql->where(implode(" OR ", $orWhereClauses));
    

    【讨论】:

      【解决方案2】:

      首先您可以生成子查询,然后获取“WHERE”部分并插入到主查询中

      $subquery = $db->select();
      $subquery->orWhere('a>10');
      $subquery->orWhere('b<20');
      etc..
      
      $subquery = $subquery->getPart(Zend_Db_Select::WHERE);
      $select->where(implode(' ',$subquery));
      

      【讨论】:

        【解决方案3】:

        对于 Zend Framework 第 2 版,情况有所不同:

        http://framework.zend.com/apidoc/2.2/classes/Zend.Db.Sql.Predicate.Predicate.html#nest

        $table->select()
             ->where(['company_id'=> $company_id])
             ->nest
                 ->where('client_email = ?', $client_email)
                 ->or
                 ->where('client_email_alt = ?', $client_email)
             ->unnest();
        

        工作正常,感觉比 ZF1 方法干净得多。

        【讨论】:

          【解决方案4】:

          您可以使用getPart()获取WHERE语句,然后连接子查询。

          $select->where('client_email = ?', $client_email)
                 ->orWhere('client_email_alt = ?', $client_email);
          
          $subquery = $select->getPart(Zend_Db_Select::WHERE);
          $select ->reset(Zend_Db_Select::WHERE);
          $select ->where('company_id = ?', $company_id)
                  ->where(implode(' ',$subquery));
          

          【讨论】:

            【解决方案5】:

            为了实现这一点,您必须在对where 方法的一次调用中构造分组子句。

            如果两个条件值相同,可以这样做:

            $select->where('client_email = ? OR client_email_alt = ?', $client_email)
            

            如果字符串中有多个占位符,DB 适配器的quoteInto 方法会将所有占位符替换为提供的值。

            如果您需要将OR 与每个字段的不同值分组,您必须手动引用这些值。这有点复杂:

            $select->where(
                $db->quoteInto('client_email = ?', $email1) . ' OR ' . $db->quoteInto('client_email_alt = ?', $email2)
            ); // $db is your instance of Zend_Db_Adapter_*
               // You can get it from a Zend_Db_Table_Abstract 
               //subclass by calling its getAdapter() method 
            

            【讨论】:

            • 我们需要的是 $select->startWhereGroup() 和 $select->endWhereGroup()。
            • Zend Framework 2 \Zend\Db\Sql\Select 在这方面看起来很有希望。
            • 太棒了!谢谢杰森,我多年来一直在寻找这个解决方案。它就像一种享受。谢谢:)
            猜你喜欢
            • 2011-09-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-10-22
            相关资源
            最近更新 更多