how to run a mysql query in yii [closed]

2019-03-31 23:34发布

I want to make mysql query to get the highest 5 values in a column from a table, so the query is :

'SELECT * FROM files ORDER BY  `uploadDate` DESC LIMIT 5'

how to run this query and save its value in a variable?

I prefer to use findAll() method with these options if it is possible.

标签: php mysql sql yii
1条回答
迷人小祖宗
2楼-- · 2019-04-01 00:25

There are several ways to achieve this, but if you prefer the query builder way

$results = Yii::app()->db->createCommand()->
          select('id, filename, uploadDate')->
          from('files')->
          order('uploadDate DESC')->
          limit(5)->
          queryAll();

var_dump($results);

Read this documentation for more detail : http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder

查看更多
登录 后发表回答