Joomla 数据库 - 如何在 getQuery 中使用 LIMIT? [英] Joomla Database - How to use LIMIT in getQuery?

查看:30
本文介绍了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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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