从 DQL 中的子查询中选择 [英] Selecting from subquery in DQL

查看:29
本文介绍了从 DQL 中的子查询中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 DQL 中的子查询结果中执行 SELECT.相当于在 SQL 中执行以下操作:

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';

我遇到的问题是它抱怨

Error: Class '(' is not defined

产生该错误的实际 DQL 是:

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?

推荐答案

使用 DQL 我很确定这是不可能的,但如果你真的需要它,你可能需要检查一下:

Using DQL I'm pretty sure that's not possible, but if you really need it you might want to check:

Doctrine Native SQL.(示例,来自同一页面的固定链接)

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

希望这有帮助...

这篇关于从 DQL 中的子查询中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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