从DQL中的子查询中选择 [英] Selecting from subquery in DQL
问题描述
我想从DQL中的子查询的结果执行SELECT。相当于在SQL中执行以下操作:
SELECT * FROM(SELECT foo1,foo2 FROM bar)其中foo1 ='something' ;
我遇到的问题是它抱怨说
错误:类'('未定义
产生该错误的实际DQL是:
SELECT u FROM(
SELECT u,COUNT(u)作为总额
FROM Utterance u LEFT JOIN u.recordings r
WHERE r.speaker IS NULL OR r.speaker<> 5
GROUP BY u.id
)匹配WHERE总计< 5
所以要重申一下,如何从子查询中执行选择? p>
使用DQL我很确定这是不可能的,但如果你真的需要它,你可能想检查:
它复杂得多,但也可以让您自由发送本地查询并执行它另一方面,如果最后一个代码段类似于您尝试实现的任何内容,那么有一个更简单的方法这不需要任何子查询:
SELECT u
FROM Utterance u LEFT JOIN u.recordings r
WHERE r.speaker IS NULL OR r.speaker<> 5
GROUP BY u.id HAVING COUNT(u)< 5
希望这有帮助...
I would like to perform a SELECT from the results of a subquery in DQL. The equivalent of doing the following in SQL:
SELECT * FROM ( SELECT foo1,foo2 FROM bar ) where foo1='something';
The problem I am running into is that it complains that
Error: Class '(' is not defined
The actual DQL that produces that error is:
SELECT u FROM (
SELECT u, COUNT(u) as total
FROM Utterance u LEFT JOIN u.recordings r
WHERE r.speaker IS NULL OR r.speaker <> 5
GROUP BY u.id
) matched WHERE total < 5
So to reiterate, how can I perform a selection from a sub query?
Using DQL I'm pretty sure that's not possible, but if you really need it you might want to check:
Doctrine Native SQL. (examples, permalink from the same page)
It's much more complex but it also gives you the freedom to send native query and execute it (the tricky part for me was object hydration).
On the other hand, if the last code segment resembles of anything what you're trying to achieve, there is a simpler way that requires no sub-queries:
SELECT u
FROM Utterance u LEFT JOIN u.recordings r
WHERE r.speaker IS NULL OR r.speaker <> 5
GROUP BY u.id HAVING COUNT(u) < 5
Hope this helps...
这篇关于从DQL中的子查询中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!