如何在yii中运行mysql查询 [英] how to run a mysql query in yii

查看:86
本文介绍了如何在yii中运行mysql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想进行mysql查询以从表中获取列中最高的5个值, 因此查询为:

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?

如果可能的话,我更喜欢将findAll()方法与这些选项一起使用.

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

推荐答案

有几种方法可以实现,但是如果您更喜欢查询生成器方法

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);

详细阅读本文档: http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder

这篇关于如何在yii中运行mysql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆