使用子选择将MySQL查询转换为Zend格式

时间:2011-07-27 07:15:09

标签: mysql zend-framework zend-db

我想将这个MySQL查询翻译成Zend格式:

SELECT count_panne, mvt, nextHost, dtmvt, stat  
FROM ( SELECT COUNT(myTable.snMCB) AS count_panne,  
        myTable.mvt,  
        myTable.nextHost,  
        myTable.dtmvt,  
        myTable.stat  
    FROM myDB.myTable myTable  
    WHERE (myTable.dtmvt = calendar__gregorian__today__)  
      AND (myTable.statut = 'NOOK')  
    GROUP BY myTable.snMCB) AS myTempTable  
 WHERE count_panne > 2  
 ORDER BY count_panne DESC   

(ps:这是一个带有modif的正确mysql语句)

我用Zend尝试了以下内容:

$subSelect = $dbTable->select()  
    ->from($dbTable,array(
        'COUNT(myTable.snMCB) AS count_panne',
        'myTable.mvt',
        'myTable.nextHost',
        'myTable.dtmvt',
        'myTable.nextHost'))
    ->where("myTable.dtmvt BETWEEN '$dateDeb' AND '$dateFin'")
    ->where('myTable.stat = ?', 'NOOK')
    ->group('myTable.snMCB')
    ;

$select = $dbTable->select()
    ->setIntegrityCheck(false)
    ->from(new Zend_Db_Expr($subSelect),
            array('count_panne',
                'mvt',
                'nextHost',
                'dtmvt',
                'statut'))
    ->where('count_panne > 2')
    ->order('count_panne DESC')
    ;

它不起作用。

1 个答案:

答案 0 :(得分:1)

您是否尝试使用db adapter direct with sql直接执行此操作...

    $db = $this->getAdapter();

$query = $db->prepare("SELECT count_panne, mvt, nextHost, dtmvt, stat  
                       FROM ( SELECT COUNT(myTable.snMCB) AS count_panne,  
                             myTable.mvt,  
                             myTable.nextHost,  
                             myTable.dtmvt,  
                             myTable.stat  
                        FROM myDB.myTable myTable  
                       WHERE (myTable.dtmvt = calendar__gregorian__today__)  
                         AND (myTable.statut = 'NOOK')  
                       GROUP BY myTable.snMCB) AS myTempTable  
                       WHERE count_panne > 2  
                    ORDER BY count_panne DESC");

        $query->execute();
        return $query->fetchAll();