简体   繁体   中英

select distinct in cdbcriteria yii

I'm still new using yii 1.6

I want input this condition in CDbCriteria

select DISTINCT substring(datetime,1,7) from sale

I try this code:

$criteria = new CDbCriteria;
$criteria->condition = 'substring(datetime,1,7) like :q';
$criteria->params = array(':q' => '%'.trim($q).'%');
$criteria->distinct = true;
$criteria->select = 'datetime';
$result = Sale::model()->findAll($criteria);

If there is date in the same month it came out twice

I try to change this

   $criteria->select = 'datetime';

to this code

 $criteria->select = 'substring(datetime,1,7)';

but it doesn't work.

Is there better way to make it work? thanks

Try this by adding the distinct function in the select portion

$criteria = new CDbCriteria;
$criteria->select = 'DISTINCT substring(datetime,1,7) AS dtime';
$result = Sale::model()->findAll($criteria)

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