问题
I am using active record. Lets call the model Product. How can i get "select min(price) from tbl_product where name like '%hair spray%'" using active record?
回答1:
You could use something like this:
$criteria = new CDbCriteria;
$criteria->select='MIN(price) as minprice';
$criteria->condition='name LIKE :searchTxt';
$criteria->params=array(':searchTxt'=>'%hair spray%');
$product = Product::model()->find($criteria);
You would just need to declare:
public $minprice; in your model class. (Using the above example.)
See docs here
来源:https://stackoverflow.com/questions/7706495/yii-get-min-max-of-a-column-using-active-record