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

廉价感情. 提交于 2019-12-12 08:35:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!