Yii - Get min, max of a column using active record

2019-04-06 08:24发布

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条回答
迷人小祖宗
2楼-- · 2019-04-06 09:25

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

查看更多
登录 后发表回答