我在ZendFramework中遇到一个MYSQL查询有问题,我希望在过去24小时内得到平均通话时间并查询如下:
SELECT AVG(duration) FROM table1 where calldate > DATE_SUB(CURDATE(), INTERVAL 24 HOUR;
这里是ZendFramework的代码:
模特:
<?php
class Model_Cdr extends Zend_Db_Table_Abstract
{
protected $_name = 'table1';
protected $_schema = 'db1';
}
控制器:
$callModel= new Model_Cdr();
$select_tot_24 = $callModel->fetchAll($callModel->select()->where("calldate > DATE_SUB(CURDATE(), INTERVAL 24 HOUR)" ));
echo $avg_24 = AVG($select_tot_24);
我也试过这个:
$callModel= new Model_Cdr();
$avg_24_1 = $callModel->fetchAll("SELECT AVG(duration WHERE calldate > DATE_SUB(CURDATE(), INTERVAL 1 HOUR))");
答案 0 :(得分:1)
使用 Zend_Db_Table_Select :
$callModel = new Model_Cdr();
$select = $callModel->select()
->from('table1', array('average' => 'AVG(duration)'))
->where('calldate > DATE_SUB(CURDATE(), INTERVAL 24 HOUR)');
$row = $callModel->fetchRow($select);
echo 'Average time: ' . $row->average;
答案 1 :(得分:1)
使用它:
$callModel = new Model_Cdr();
$select = $callModel->select()
->from($callModel, array('average' => 'AVG(duration)'))
->where('calldate > DATE_SUB(CURDATE(), INTERVAL 24 HOUR)');
$row = $callModel->fetchRow($select);
echo 'Average time: ' . $row->average;