Yii:按日期计算汇总

时间:2016-10-04 11:41:26

标签: php mysql yii

我在Yii 1.1中有一个简单的模型:

Expense

amount: int
date: string

如何使用内置Yii功能按 date 计算聚合?

换句话说我想使用Yii获得以下查询的结果:

SELECT
  date, SUM(amount) AS dateAmount

FROM
  tbl_expense

GROUP BY
  date

ORDER BY
  date DESC

3 个答案:

答案 0 :(得分:3)

我已经探索了两种执行查询的方法,

首先:

$list= Yii::app()->db->createCommand('SELECT date, SUM(amount) AS dateAmount
FROM tbl_expense
GROUP BY date
ORDER BY date DESC')->queryAll();
var_dump($list->getData());

第二: (通常用于向Gridview和Listview提供数据提供者)

$select = "SELECT date, SUM(amount) AS dateAmount
FROM tbl_expense
GROUP BY date
ORDER BY date DESC";

$dataProvider=new CSqlDataProvider($select,
array(
    'pagination'=>false,
    'params'=>$sqlParam
   )
);
var_dump($dataProvider->getData());

答案 1 :(得分:1)

您可以使用标准

$criteria = new CDbCriteria();
$criteria->select = 'date, SUM(amount) dateAmount';
$criteria->group = 'date';


$model=Expense::model()->find($criteria);

答案 2 :(得分:0)

您可以使用条件

context

AND 将属性React.cloneElement添加到模型类。

$criteria = new CDbCriteria();
$criteria->select = 'date, SUM(amount) dateAmount';
$criteria->group = 'date';


$model=Expense::model()->find($criteria);