how to run a mysql query in yii(如何在 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
Read this documentation for more detail : http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder
这篇关于如何在 yii 中运行 mysql 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 yii 中运行 mysql 查询
基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01