JOIN中的Doctrine 2子查询

时间:2019-02-06 17:33:07

标签: mysql doctrine symfony-3.4 doctrine-query

我正在尝试将此SQL转换为学说查询生成器,但无法在联接中创建子查询。实际的查询包含多个联接,但是我只想一种需要在联接中包含子查询的方法。

查询:

SELECT id, venue_name, mediahawk_clicks
FROM location LEFT OUTER JOIN 
(SELECT location_id, sum(total_calls) as mediahawk_clicks FROM mediahawk_stat GROUP BY location_id) AS calls
ON location.id = calls.location_id

这是我尝试过的!

        $em = $this->getEntityManager();
        $mediaHawkSubSelect = $em->createQueryBuilder();
        $mediaHawkSubSelect->select("ms.locationId, SUM(ms.totalCalls) as mediahawkClicks")
        ->from('AppBundle:MediahawkStat', 'ms');

        $q = $em->createQueryBuilder();
        $q->from('AppBundle\Entity\Location', 'q')
            ->select("q.id, q.venueName");
        $q->where('q.publishedStatus = 1')
        $q->groupBy('q.id');

        $q->leftJoin(sprintf("(%s)", $mediaHawkSubSelect->getQuery()->getSQL()).")", 'md','WITH', 'q.id = md.locationId')
            ->addSelect('SUM(md.totalCalls) as mediahawkClicks');

这是形成的DQL:

SELECT q.id, q.venueName, SUM(md.totalCalls) as mediahawkClicks
FROM AppBundle\Entity\Location q INNER JOIN
(SELECT m0_.location_id AS location_id_0, SUM(m0_.total_calls) AS sclr_1 FROM mediahawk_stat m0_)) md 
WITH q.id = md.locationId GROUP BY q.id

但是当我尝试获取查询结果时。它给出错误信息:

[Semantical Error] line 0, col 108 near '(SELECT m0_.location_id': Error: Class '(' is not defined.

将上述查询转换为Doctrine查询生成器的任何帮助将受到赞赏。

谢谢

0 个答案:

没有答案