【问题标题】:Zend Framework Select Objects And UNION()Zend 框架选择对象和 UNION()
【发布时间】:2010-11-22 02:53:44
【问题描述】:

我很确定这在 Zend Framework 中是不可能的(我已经搜索了 Web、文档和问题跟踪器),但我只是想确定一下,所以我在这里问。

$select = $this->select();
$select->union($select1, $select2);

这当然行不通。解释我需要什么。我需要使用 UNION() 在 SELECT 查询中合并 2 个表,我知道我可以这样做:

$select = "$select1 UNION $select2";

问题是它会返回一个字符串,我需要获取一个选择对象,以便可以将它与 Zend_Paginator 一起使用。

我已经通过修改我的数据库架构解决了这个问题,但我只是好奇是否有一些解决方法。

【问题讨论】:

    标签: php mysql zend-framework select union


    【解决方案1】:

    这是我为建立联合所做的工作:

    $select = $this->select();
    //common select from both sides of the union goes here
    
    $select1 = clone($select);
    //select1 specifics here
    
    $select2 = clone($select);
    //select 2 specifics here
    
    $db = $this->getAdapter();
    $pageselect = $db->select()->union(array("($select1)", "($select2)"));
    

    记住Db_Select__toString会打印出那个select生成的SQL,帮助你调试。

    【讨论】:

      【解决方案2】:

      Zend_Db_Select has a union method 所以我认为这是可能的,如果您可以使用选择对象构建您的查询。我没有将 Zend_Db_Select (或表子类)与 union 一起使用,但我想你可以做类似的事情

      $select = $this->select()
                     ->where('blah')
                     ->union($sql);
      

      【讨论】:

        【解决方案3】:

        一个完整的例子:

         public function getReservationById($id)
         {
          if(!$id) return null;
        
          $sql = $this->table->select();
          $sql->union(array(
           $this->table->select()->where('id=?', $id),
           $this->tableFinished->select()->where('id=?', $id),
           $this->tableCanceled->select()->where('id=?', $id),
           $this->tableTrashed->select()->where('id=?', $id)
           ));
          echo $sql->__toString();
         }
        

        以及生成的查询:

        SELECT reservations.* FROM reservations WHERE (id='5658') UNION SELECT res_finished.* FROM res_finished WHERE (id='5658') UNION SELECT res_cancel.* FROM @987654327 @ WHERE (id='5658') UNION SELECT res_trash.* FROM res_trash WHERE (id='5658')

        【讨论】:

          【解决方案4】:

          这个实际示例显示了一个函数,该函数返回最新的行集,或者返回特定年份的可用收藏博客条目(艺术博客):

          public function fetchBestOf($year)
          {
              $selectLatest = $this->select()->where('isHidden = 0')
                                             ->where('YEAR(dateCreated) = ' . $year)
                                             ->where('isHighlight = 0');
              $selectHighlights = $this->select()->where('isHidden = 0')
                                                 ->where('YEAR(dateCreated) = ' . $year)
                                                 ->where('isHighlight = 1');
          
              $selectUnion = $this->select()->union(array($selectLatest, $selectHighlights), Zend_Db_Select::SQL_UNION_ALL)
                             ->order('isHighlight DESC')
                             ->order('dateCreated DESC')
                             ->order('workID DESC')
                             ->limit('5');
          
              $rowset = $this->fetchAll($selectUnion);
              return $rowset;
          }
          

          【讨论】:

            【解决方案5】:

            Zend 建议的最佳方式如下......

            $sql = $this->_db->select()
                ->union(array($select1, $select2,$select3))
                        ->order('by_someorder');
            
            echo $sql->__toString();
            
            $stmt = $db->query($sql);
            $result = $stmt->fetchAll();
            

            echo 将显示查询

            这里 $select1, $select2, $select3 可以是不同的选择查询 列数...

            【讨论】:

              【解决方案6】:

              这对我来说是这样的:

              $select1 = $this->select();               
              $select2 = $this->select();
              

              在两个查询中获取必要的数据后,UNION 语法如下所示:

               $select = $this->select()->union(array('('.$select1.')', '('.$select2.')'));  
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2017-10-20
                • 1970-01-01
                • 1970-01-01
                • 2011-02-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多