我正在运行自定义查询,没有涉及模型,它返回一个空数组。
以下是我使用的确切代码:
$query = 'SELECT SUM(open_diff) opens, SUM(revenue_diff) revenue, SUM(revenue) real_rev, manual_rev, SUM(opens) actual_opens
FROM data.discrepancy
WHERE discrepancy_date >= \''.$dateStart.'\' AND discrepancy_date <= \''.$dateEnd.'\' AND feed_id = '.$feeds[$i]["feed_id"];
$db = Zend_Registry::get('db_slave');
$stmt = $db->query($query);
$records = $stmt->fetchAll();
Zend_Debug::dump($records); gets me this result:
array(1) {
[0] => array(5) {
["opens"] => NULL
["revenue"] => NULL
["real_rev"] => NULL
["manual_rev"] => NULL
["actual_opens"] => NULL
}
}
数据在数据库中,如果我直接在MySql中运行该查询,我没有问题。
请告知。
答案 0 :(得分:2)
如果没有匹配的记录,MySQL将为sum()返回null。正在执行的最终查询是什么(评估变量)?
我也尝试直接在MySQL中运行它,你可能会得到相同的结果。
var_dump($dateStart, $dateEnd, $feeds[$i]['feed_id']);
查看其中包含的内容,您可能会看到问题。
答案 1 :(得分:0)
尝试以下代码
$dbAbstract = new Zend_Db_Table_Abstract();
$select = $dbAbstract->select()
->from(
array('a' => 'data.discrepancy'),
array('SUM(open_diff) AS opens', 'SUM(revenue_diff) AS revenue'), 'SUM(revenue) AS real_rev', 'SUM(opens) AS actual_opens'
)
->where('discrepancy_date >=?', $dateStart)
->where('discrepancy_date <=?', $dateEnd)
->where('feed_id =?', $feeds[$i]["feed_id"]);
$result = $dbAbstract->fetchAll($select);
请删除无法使用的AS