Joomla 数据库 - 如何在 getQuery 中使用 LIMIT?

2022-01-06 00:00:00 mysql joomla2.5 joomla joomla3.1 joomla3.0

我想使用 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);

阅读更多

相关文章