我在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))");

最佳答案

用它:

$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;

关于php - 最近24小时的平均通话时间ZendFramework,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28249816/

10-09 21:57