2016-10-18 47 views
0

我有2個選擇,這是聯合在一起,這樣的事情:的Zend DB工會計數

$select->from (array (
       'A' => 'tableA' 
     ), array (
       'field1', 
       ..... 
     )); 
$select2->from (array (
       'B' => 'tableB' 
     ), array (
       'field1', 
       ..... 
     )); 
$select3 = $this->getAdapter()->select() 
      ->union(array($select1, $select2)); 

現在的基礎上,工會,我想這樣做:

$select3->reset ('columns')->columns (new Zend_Db_Expr ('COUNT(DISTINCT(field1))')) 

但我「M收到此錯誤:

No table has been specified for the FROM clause

這在技術上是正確的,但我並不想從表計,我想從結果在我的工會計數。我怎麼做?

回答

0

我結束了各地的工作這樣的:

$select3->reset ('columns');//->columns (new Zend_Db_Expr ('COUNT(DISTINCT(field1))')) 
$sql = $select->__toString(); 
     $countQuery = <<<EOD 
SELECT COUNT(DISTINCT(res.field1)) as total_rows FROM ($sql) as res 
EOD; 


     $countResult = $this->getAdapter()->query($countQuery)->fetchAll(); 

     $count = 0; 

     if (! empty ($countResult)) { 
      $count = $countResult [0] ['total_rows']; 
     }