我正在尝试使用Doctrine 2实现复杂的SQL查询。 但是,我的SQL查询有子选择。我认为这是'问题,但我无法实现解决这个问题的方法。
SELECT
x.year, x.month, y.total
FROM
(SELECT
YEAR(a.date) year, MONTH(a.date) month, count(*) number
FROM
Acessos a
GROUP BY
YEAR(a.date), MONTH(a.date)) x
INNER JOIN
(SELECT
MAX(Z.number) total
FROM
(SELECT
COUNT(a.id) number
FROM
Acessos a
GROUP BY
YEAR(a.date), MONTH(a.date)) z) y ON x.number = y.total LIMIT 1
当我将此查询用于运行trhu代码时,我得到QueryException
。
<b>Fatal error</b>: Uncaught exception 'Doctrine\ORM\Query\QueryException'
with message 'SELECT x.messelecionado, x.anoselecionado, y.total from (select
month(a.data) messelecionado, year(a.data) anoselecionado, count(*) numero from
Acesso a group by month(a.data), year(a.data)) x inner join (select max(z.numero)
total from (select count(a.id) numero from Acesso a group by month(a.data),
year(a.data)) z) y on x.numero = y.total limit 1' in
C:\xampp\htdocs\patclin\app\Doctrine\ORM\Query\QueryException.php:39
Stack trace:
#0 C:\xampp\htdocs\patclin\app\Doctrine\ORM\Query\Parser.php(429):
Doctrine\ORM\Query\QueryException::dqlError('SELECT x.messel...')
#1 C:\xampp\htdocs\patclin\app\Doctrine\ORM\Query\Parser.php(854):
Doctrine\ORM\Query\Parser->semanticalError('Class '(' is no...', Array)
#2 C:\xampp\htdocs\patclin\app\Doctrine\ORM\Query\Parser.php(1529):
Doctrine\ORM\Query\Parser->AbstractSchemaName()
#3 C:\xampp\htdocs\patclin\app\Doctrine\ORM\Query\Parser.php(1426):
Doctrine\ORM\Query\Parser->RangeVariableDeclaration()
#4 C:\x in <b>C:\xampp\htdocs\patclin\app\Doctrine\ORM\Query\QueryException.php</b>
on line <b>49</b><br />
任何人都知道我该怎么做?
感谢!!!
答案 0 :(得分:3)
您使用group by子句编写的YEAR函数不会被学说理解。你必须为此注册自定义dql函数。 这是doctrine提供的dql函数列表: http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#dql-functions
这是创建自己的dql函数的链接 http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#adding-your-own-functions-to-the-dql-language
groupby仅适用于聚合函数(AVG,COUNT,MIN,MAX,SUM)
此链接可能对您有所帮助 Group by using month, year in doctrine2