FLOW3 Doctrine2 :: SELECT b,count(b) FROM (SELECT a,b FROM x\y\z GROUP BY a) GROUP BY b

发布于 2024-12-14 11:53:13 字数 1320 浏览 6 评论 0原文

我对 FLOW3 很陌生,对 Doctrine 也很陌生。

我只是运行一些测试,我想学习或理解一些稍后会使用的技术。现在我被这个学说部分困住了,我想生成一些统计数据。

$results = $this->entityManager
    ->createQuery('SELECT version,count(version) 
        FROM (SELECT device, version 
            FROM \MyStuff\Stats\Domain\Model\Stat 
            WHERE 
                date > \'2011-10-01 00:00:00\' and 
                date < \'2011-10-02 00:00:00\' 
            GROUP BY device) GROUP BY version')
    ->getResult();

我也在其他地方询问过,他们将我引导到 学说文档。 好吧,现在有几个例子,但是这两个衬里是微不足道的,我找不到任何与这种子选择相关的例子。
所以我希望这里有人可以帮助我。

编辑:
我想使用 dql 解决这个问题。
我尝试使用 querybuilder 解决此问题,但有人告诉我 querybuilder != dql

Edit 2:
现在我被告知,doctrine2 不支持“FROM (SUBSELECT)”中的子选择,但它支持“... WHERE IN (SUBSELECT)”,并且可以将我的查询重写为 IN () 形式。嗯,现在就想办法解决这个问题。

编辑3: 我无法将 from-subquery 重写为 in-subquery。所以... dql 不执行子查询,并且没有其他方法可以用 dql 执行我想要的操作?那么 dql 就会缺少一个我想说的非常重要的功能。或者我只是没有看到某事。这里 ?

编辑4: 我终于得到了子查询,但速度慢了大约 10 倍(4 秒而不是 0.4 秒),现在 #doctrine 的一些学说人员告诉我,我应该使用 nativeQuery 函数。

编辑5: 它现在使用 nativeQuery 工作,请参阅我的答案......

I'm quite new to FLOW3 and I'm very new to Doctrine.

I'm just running some tests, and I want to learn or understand some techniques that I'll use later on. Now I'm stuck with this doctrine part where I want to generate some stats.

$results = $this->entityManager
    ->createQuery('SELECT version,count(version) 
        FROM (SELECT device, version 
            FROM \MyStuff\Stats\Domain\Model\Stat 
            WHERE 
                date > \'2011-10-01 00:00:00\' and 
                date < \'2011-10-02 00:00:00\' 
            GROUP BY device) GROUP BY version')
    ->getResult();

I asked at other places too, where they directed me to the Doctrine Docs.
Well, now there are several examples but these 2 liners are trivial and I couldn't find any example related to that kind of subselect.
So I hope someone here can help me out.

Edit :
I'd like to solve this using dql.
I tried to solve this using a querybuilder but I was told that querybuilder != dql

Edit 2:
Now I was told that doctrine2 doesn't support subselects in "FROM (SUBSELECT)" but that it does "... WHERE IN (SUBSELECT)" and that one could rewrite my query to the IN () form. Well, trying to figure that out now.

Edit 3:
I'm failing in rewriting the from-subquery into an in-subquery. So... dql doesn't do subqueries and there is no other way to do what I want with dql ?! Then dql would lack a very important feature I'd say. Or am I just not seeing sth. here ?

Edit 4:
I finally got the in-subquery but it was about 10 times slower (4 seconds instead of 0.4) and now I was told by some doctrine guys from #doctrine, that I should use the nativeQuery function instead.

Edit 5:
It works using the nativeQuery now, see my answer for that...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小梨窩很甜 2024-12-21 11:53:13

使用本机查询,它的工作原理如下......

$rsm = new \Doctrine\ORM\Query\ResultSetMapping();
$rsm->addScalarResult('version', 'version');
$rsm->addScalarResult('count', 'count');
$results = $this->entityManager
    ->createNativeQuery(
        'SELECT version, COUNT(version) as count FROM
        (
            SELECT device, version 
            FROM mystuff_stats_domain_model_stat 
            WHERE 
                date > \'2011-10-01 00:00:00\' and 
                date < \'2011-10-02 00:00:00\' 
            GROUP BY device
        ) 
        as devices GROUP BY version',$rsm)
    ->execute();
echo var_dump($results,true);

Using a native query it works like that...

$rsm = new \Doctrine\ORM\Query\ResultSetMapping();
$rsm->addScalarResult('version', 'version');
$rsm->addScalarResult('count', 'count');
$results = $this->entityManager
    ->createNativeQuery(
        'SELECT version, COUNT(version) as count FROM
        (
            SELECT device, version 
            FROM mystuff_stats_domain_model_stat 
            WHERE 
                date > \'2011-10-01 00:00:00\' and 
                date < \'2011-10-02 00:00:00\' 
            GROUP BY device
        ) 
        as devices GROUP BY version',$rsm)
    ->execute();
echo var_dump($results,true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文