原则-从表中选择最后4行 [英] Doctrine - select last 4 rows from table

查看:106
本文介绍了原则-从表中选择最后4行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony / Doctrine。
我正在尝试从表中选择最后4行,但是我收到了错误消息。

I'm using Symfony/Doctrine. I'm trying to select last 4 rows from table, but im getting error.

$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery(
'SELECT c FROM DprocMainBundle:Courses c ORDER BY id DESC LIMIT 4'
);

$course = $query->getResult();

这是我的查询,但显示错误。

This is my query but it shows error.


预期的字符串结尾,得到了'LIMIT'

Expected end of string, got 'LIMIT'

我应该如何使用限制,并获得最后4行?

How should i use limit, and get the LAST 4 rows?

谢谢!

推荐答案

使用 setMaxResults()限制结果数。

$course = $query->setMaxResults(4)->getResult();

如果要将其用于分页,可以添加 setFirstResult() 调用。

If you want to use this for pagination you can add a setFirstResult() call.

 $course = $query->setMaxResults(4)->setFirstResult(10)->getResult();

这篇关于原则-从表中选择最后4行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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