2014-03-29 59 views
1

我有一個MySQL查詢,在PhpMyAdmin中完美工作,但在Zend Framework 1.12及其方法中使用時引發SQLSTATE[HY000]: General error。那就是:SQLSTATE [HY000]:ZF1中MySQL變量的一般錯誤

SET @csum := 0; 
SELECT s1.Date, (@csum := @csum + s1.total) as cumulative_sum 
FROM (
     SELECT c.datefield AS Date, IFNULL(SUM(subscription_total),0) AS total 
     FROM subscription s 
     RIGHT JOIN calendar c 
     ON c.datefield = DATE(subscription_date) 
     WHERE (c.datefield BETWEEN 
      (SELECT MIN(DATE(subscription_date)) FROM subscription) 
      AND 
      NOW() 
     ) 
     GROUP BY DATE 
    ) AS s1 

如果我刪除了SET聲明,但我需要設置一個MySQL變量,否則cumulative_sum將只是NULL值的語句不返回任何錯誤。下面是該方法的代碼:

public function findCumulativeCashflow($statut) 
{ 
    $db = $this->_getDbTable(); 

    $dbAdapter = new Zend_Db_Adapter_Pdo_Mysql($db->getAdapter()->getConfig()); 

    $sql = '<SQL statement above>'; 

    $statement = $dbAdapter->query($sql); 

    $rows = $statement->fetchAll(); 

    return $rows; 
} 

錯誤點$rows = $statement->fetchAll();線,有另一種方法ZF與SET @var := value使用?

任何想法將不勝感激。 謝謝!

回答

2

根據this question,我需要使用另一個查詢,因爲fetchAll()無法處理多個查詢。

因此,我需要設置MySQL變量$dbAdapter->query('SET @csum := 0;');,然後執行主查詢。

相關問題