简体   繁体   中英

Write Query in CDBCriteria in Yii with Where and Group By clause

I have this query, I want to do this query using CDbCriteria. Model is 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

This is what i have tried so far.. is it a proper way to use WHERE, GROUPBY, HAVING COUNT.. in CDbCriteria.. This is my search function in a model 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;

Any suggestions, how to achieve this in Search function of model, because i want to show the result in CGridView...

The field you group by "c.calls_id" should exist in your select statement

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

The proper statement for "having" is, for example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM