2017-01-10 107 views
2

我有以下情況。 我需要進行查詢,並且我不知道如何在DQL中嵌套子查詢。在DQL上用Symfony查詢子查詢

我有一個用戶表。 在該表中有賣家和代表。 用戶有:

---- 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個單個查詢中完成?

回答

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(); 
} 
+0

是完美的。謝謝!!! – Javier