在DQL上使用Symfony查询子查询

时间:2017-01-10 10:08:16

标签: php mysql symfony dql

我有以下情况。 我需要进行查询,而且我不知道如何在DQL中嵌套子查询。

我有一张用户表。 在该表中有卖家和代表。 用户有:

---- ID,
---- Name
---- Profile (ROLE_REPRESENTATIVE, ROLE_SELLER)
---- Status (1 = DECLINE | 2 = ON_HOLD | 3 = APPROVED)

示例用户表:

--------- ID ----------- Name ------------------ PROFILE ------------ STATUS ---- REPRESENTATIVE_ID ----
    ------ 1 -------- Representative_1 ------ ROLE_REPRESENTATIVE ------ 3 ----------- NULL -------------
    ------ 2 -------- seller_1 -------------- ROLE_SELLER -------------- 3 ------------ 1 ---------------
    ------ 3 -------- seller_2 -------------- ROLE_SELLER -------------- 3 ------------ 1 ---------------
    ------ 4 -------- seller_3 -------------- ROLE_SELLER -------------- 2 ------------ 1 ---------------
    ------ 5 -------- Representative_2 ------ ROLE_REPRESENTATIVE ------ 3 ----------- NULL -------------
    ------ 6 -------- Representative_3 ------ ROLE_REPRESENTATIVE ------ 3 ----------- NULL -------------
    ------ 7 -------- seller_4 -------------- ROLE_SELLER -------------- 2 ------------ 5 ---------------
    ------ 8 -------- seller_5 -------------- ROLE_SELLER -------------- 2 ------------ 5 ---------------
    ------ 9 -------- seller_6 -------------- ROLE_SELLER -------------- 2 ------------ 5 ---------------
    ----- 10 -------- seller_7 -------------- ROLE_SELLER -------------- 3 ------------ 1 ---------------
    ----- 11 -------- seller_8 -------------- ROLE_SELLER -------------- 3 ------------ 1 ---------------

卖方有代表(代表),这是用户和用户之间的1-N关系。

然后,我需要获得所有代表(id +名称)和未批准的卖家总数(状态= 2)(状态= 2 = ON_HOLD)

获取代表

SELECT representatives
FROM AppBundle: User representatives
WHERE
    Representatives.profile = 'ROLE_REPRESENTATIVE'
GROUP BY representatives.id
ORDER BY representatives.id DESC

让卖家“等待”批准

SELECT sellers
FROM AppBundle: User sellers
WHERE
    Sellers.profile = 'ROLE_SELLER' AND
    Sellers.status = 2
GROUP BY sellers.id
ORDER BY sellers.id DESC

最后,我必须知道如何将其结合起来:

----- ID ----------- Name --------------- TotalSellersOnHold -----
------ 1 -------- Representative_1 ------------- 40 -----------------
------ 5 -------- Representative_2 ------------- 27 -----------------
------ 6 -------- Representative_3 ------------- 12 -----------------

如何在一个查询中完成此查询?

1 个答案:

答案 0 :(得分:1)

sql命令应该是这样的:

SELECT r.ID, r.Name, COUNT(DISTINCT s.id) as 'TotalSellerOnHold' FROM users r
INNER JOIN users s ON s.REPRESENTATIVE_ID = r.id
WHERE r.TYPE = 'ROLE_REPRESENTATIVE'
AND s.STATUS = 2
AND s.TYPE = 'ROLE_SELLER'
GROUP BY r.ID
ORDER BY r.ID DESC;

在DQL中(假设您进入自定义存储库)

//AppBundle/Repository/MyRepository.php
public function getAllRepresentativesWithOnSellers()
{
    $query = $this->createQueryBuilder('r')
        ->select('r.id, r.name')
        ->addSelect('COUNT(DISTINCT s.id)')
        ->innerJoin('AppBundle\Entity\User', 's')
        ->where('r.type = ROLE_REPRESENTATIVE')
        ->andWhere('s.status = 2')
        ->andWhere('s.type = ROLE_SELLER')
        ->groupBy('r.id')
        ->orderBy('r.id', 'desc')
        ->getQuery()
    ;

    return $query->getResult();
}