Joomla Database - How to use LIMIT in getQuery?(Joomla 数据库 - 如何在 getQuery 中使用 LIMIT?)
问题描述
我想使用 joomla 内置数据库类构建以下查询.
I want to build the below query using joomla inbuilt database class.
SELECT *
FROM table_name
ORDER BY id DESC
LIMIT 1
这是我目前建立的查询.
This is the query I have built up to now.
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select($db->nameQuote('*'));
$query->from($db->nameQuote(TABLE_PREFIX.'table_name'));
$db->setQuery($query);
$rows = $db->loadObjectList();
我不知道如何将限制(LIMIT 1)添加到查询中.有人可以告诉我怎么做吗?谢谢
I don't know how to add the limit(LIMIT 1) to the query. Can someone please tell me how to do it? Thanks
推荐答案
早于 Joomla 3.0
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from($db->nameQuote('#__table_name'))
->order($db->nameQuote('id').' desc');
$db->setQuery($query,0,1);
$rows = $db->loadObjectList();
$db->setQuery
函数接受 3 个参数.第一个是查询,然后是开始,然后是限制.我们可以限制记录,如上所示.
$db->setQuery
function takes 3 parameters. The first one being the query, then the start, then the limit. We can limit records as shown above.
setLimit(integer $limit, integer $offset)
如果你只想要一行
$query->setLimit(1);
阅读更多
这篇关于Joomla 数据库 - 如何在 getQuery 中使用 LIMIT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Joomla 数据库 - 如何在 getQuery 中使用 LIMIT?


- SQL 临时表问题 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01