我正在使用Doctrine和DQL函数createQuery()
,我有这个查询:
SELECT id, 1 + (
SELECT count( * )
FROM user a
WHERE a.points > b.points ) AS RNK, points
FROM user b
此查询返回使用字段points
计算的排名。
如何转换此查询以在createQuery()中使用?
现在我有:
$query = $this->getEntityManager()->createQuery("
SELECT id, 1+(
SELECT count(*)
from AppBundle\Entity\User a
WHERE a.points > b.points) as RNK, points
FROM AppBundle\Entity\User b
");
但是这会返回语法错误:"[Syntax Error] line 0, col 14: Error: Expected Literal, got 'SELECT'"
答案 0 :(得分:0)
试试这个:
$query = $this->getEntityManager()->createQuery("
SELECT b.id, 1+(
SELECT count(a)
from AppBundle\Entity\User a
WHERE a.points > b.points) as RNK, points
FROM AppBundle\Entity\User b");
答案 1 :(得分:0)
因为你想得到同一个实体(USER)的编号,为什么不简单地做:
$query = $this->getEntityManager()->createQuery("
SELECT u.id, 1+count(u.*) as RNK, u.points FROM AppBundle\Entity\User u
");
来自doc:doctrine chap 14.2.4:
$query = $em->createQuery('SELECT COUNT(u.id) FROM Entities\User u');