我正在尝试使用原则 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 'DoctrineORMQueryQueryException'
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:xampphtdocspatclinappDoctrineORMQueryQueryException.php:39
Stack trace:
#0 C:xampphtdocspatclinappDoctrineORMQueryParser.php(429):
DoctrineORMQueryQueryException::dqlError('SELECT x.messel...')
#1 C:xampphtdocspatclinappDoctrineORMQueryParser.php(854):
DoctrineORMQueryParser->semanticalError('Class '(' is no...', Array)
#2 C:xampphtdocspatclinappDoctrineORMQueryParser.php(1529):
DoctrineORMQueryParser->AbstractSchemaName()
#3 C:xampphtdocspatclinappDoctrineORMQueryParser.php(1426):
DoctrineORMQueryParser->RangeVariableDeclaration()
#4 C:x in <b>C:xampphtdocspatclinappDoctrineORMQueryQueryException.php</b>
on line <b>49</b><br />
有人知道我能做什么吗?
谢谢!!!
您用 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)
此链接可能会对您有所帮助在学说中按月、年分组2