2015-07-01 34 views
0

條件我有一個這樣的DQL:DQL用SUM

$dql = "SELECT d.name, " 
. "SUM(m.id and m.status = 'a') members, " 
. "SUM(m.position_id = 19 and m.status = 'a') subcon, " 
. "SUM(m.position_id = 20 and m.status = 'a') ojt, " 
. "SUM(m.status = 'h') hr_intra " 
. "FROM OssOrgBundle:User m " 
. "RIGHT JOIN OssOrgBundle:Department d " 
. "ON m.department = d.id " 
. "GROUP BY d.id"; 

$qb = $em->createQuery($dql); 
deptListCount = $qb->getResult(); 

做內部具有條件的學說支持聚合函數SUM? 我試過SQL中的查詢,並且它可以工作。 我無法將其轉換成DQL

我也做過這樣使用:

$deptListCount = $em->createQueryBuilder("r") 
->select("d.name") 
->addSelect("SUM(m.id AND m.status = 'a') members") 
->addSelect("SUM(m.position_id = 19 and m.status = 'a') subcon") 
->addSelect("SUM(m.position_id = 20 and m.status = 'a') ojt") 
->addSelect("SUM(m.status = 'h') hr_intra ") 
->from("OssOrgBundle:Department", "d") 
->leftJoin("OssOrgBundle:User", "m", "WITH", "m.deprtment = :id") 
->groupBy("d.id") 
->getQuery() 
->getResult(AbstractQuery::HYDRATE_ARRAY); 

仍然沒有工作

回答

0

沒有教義,你需要使用case when有條件sum

sum(case when m.position_id = 19 and m.status = 'a' then 1 else 0 end) as subcon 

DQL中其餘部分的更改以及

m.id AND m.status = 'a'不知道你想申請我假定你正在尋找的東西作爲

sum(case when m.id is not null and m.status = 'a' then 1 else 0 end) as members 
+0

什麼邏輯非常感謝你的回答 – kimondoe