Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 227
Mukesh Soni Avatar asked Oct 09 '11 20:10

Mukesh Soni


1 Answers

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

like image 84
ldg Avatar answered Nov 15 '22 15:11

ldg