簡體   English   中英

Zend DB聯合計數

[英]Zend DB union count

我有2個選擇,它們是聯合的,如下所示:

$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))' ))

但我收到此錯誤:

沒有為FROM子句指定表

從技術上講這是正確的,但是我不希望從表中獲得計數,而是希望從聯合中的結果中獲得計數。 我怎么做?

我最終像這樣解決它:

$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'];
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM