使用Where和Group By子句在Yii的CDBCriteria中写入查询

时间:2016-08-03 09:22:15

标签: php mysql yii

我有这个查询,我想使用CDbCriteria进行此查询。模型是Clog

SELECT c.id, c.answer, c.number
FROM `Clog`as c
WHERE c.company_id =20
AND date(c.answer) = '2016-04-02'
GROUP BY c.calls_id
HAVING count(c.id) <2

这是我到目前为止所尝试过的..它是一种在CDbCriteria中使用WHERE,GROUPBY,HAVING COUNT ..的正确方法..这是我在模型Calls中的搜索功能

$criteria = new CDbCriteria();
$criteria->select = 'c.id, c.answer, c.number';
$criteria->addCondition(' c.company_id='.$this->companyId);
$criteria->addBetweenCondition(' c.answer', ''.$this->start_date.'', ''.$this->end_date.'');
$criteria->group = 'c.calls_id';
$criteria->limit < 2;

任何建议,如何在模型的搜索功能中实现这一点,因为我想在CGridView中显示结果......

1 个答案:

答案 0 :(得分:0)

您分组的字段&#34; c.calls_id&#34;应存在于您的select语句中

$criteria->select = 'c.id, c.answer, c.number, c.calls_id';

&#34;有&#34;的正确陈述例如:

$criteria->having = "count(c.id) < 2";