在 DQL 上通过 Symfony 使用子查询进行查询
Query with subquery over Symfony on DQL
我有以下情况。
我需要做一个查询,但我不知道如何在DQL中嵌套子查询。
我有 table 个用户。
在 table 中有卖方和代表。
用户有:
---- ID,
---- Name
---- Profile (ROLE_REPRESENTATIVE, ROLE_SELLER)
---- Status (1 = DECLINE | 2 = ON_HOLD | 3 = APPROVED)
示例用户 Table:
--------- 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 ---------------
卖家有代表(representatives),Users与Users之间是1-N关系
然后,我需要得到所有的代表(id + name)和未通过审核的卖家总数(status = 2)(status = 2 = ON_HOLD)
获取代表
SELECT representatives
FROM AppBundle: User representatives
WHERE
Representatives.profile = 'ROLE_REPRESENTATIVE'
GROUP BY representatives.id
ORDER BY representatives.id DESC
获得卖家"pending"批准
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 个查询中完成此查询?
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();
}
我有以下情况。 我需要做一个查询,但我不知道如何在DQL中嵌套子查询。
我有 table 个用户。 在 table 中有卖方和代表。 用户有:
---- ID,
---- Name
---- Profile (ROLE_REPRESENTATIVE, ROLE_SELLER)
---- Status (1 = DECLINE | 2 = ON_HOLD | 3 = APPROVED)
示例用户 Table:
--------- 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 ---------------
卖家有代表(representatives),Users与Users之间是1-N关系
然后,我需要得到所有的代表(id + name)和未通过审核的卖家总数(status = 2)(status = 2 = ON_HOLD)
获取代表
SELECT representatives
FROM AppBundle: User representatives
WHERE
Representatives.profile = 'ROLE_REPRESENTATIVE'
GROUP BY representatives.id
ORDER BY representatives.id DESC
获得卖家"pending"批准
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 个查询中完成此查询?
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();
}