我有一个MySQL查询在PhpMyAdmin中完美运行,但在Zend Framework 1.12及其SQLSTATE[HY000]: General error
方法中使用时会抛出fetchAll()
。这是:
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
?
任何想法都会非常感激。 谢谢!
答案 0 :(得分:2)
根据this question,我需要使用另一个查询,因为fetchAll()无法处理多个查询。
因此,我需要使用$dbAdapter->query('SET @csum := 0;');
设置MySQL变量,然后执行主查询。