2016-11-10 72 views
0

我需要通過像的多個字段中的教義來篩選下列:學說濾波

SELECT company , state 
FROM employees 
WHERE 
(company, state) <> ('xxxxx', 'xxxx') 
AND 
(company, state) <> ('xxxx', 'xxxx') 
GROUP BY company, state 

我嘗試以下方法:

$qb->andWhere($qb->expr()->andX($qb->expr()->neq('b.company',"'".$i['description']."'"), $qb->expr()->neq('b.state', "'".$i['state']."'"))); 

但結果是不期望的:

(company <> 'xxxxx' AND state <> 'xxxx') AND (company <> 'xxxxx' AND state <> 'xxxxx') 

我該如何做第一個通過學說? 問候!

回答

0

上面的查詢可以作爲兩個列表的OR來工作,這會讓事情變得更簡單。

E.g.

WHERE狀態NOT IN的國家或公司NOT IN公司

彷彿要麼是真的,那麼公司+狀態的該員工的組合是不是排除一個。

鑑於這種情況,你可以如下面這條命令:

$qb = $this->getEntityManager()->createQueryBuilder(); 

    $excludedCompanies = ['company1', 'another company', 'company etc']; 
    $excludedStates = ['state1', 'another state', 'state etc']; 

    return $qb->select('e.company, e.state') 
     ->from('YourBundle:Employee', 'e') 
     ->where($qb->expr()->notIn('e.company', ':excludedCompanies')) 
     ->orWhere($qb->expr()->notIn('e.state', ':excludedStates')) 
     ->setParameter('excludedCompanies', $excludedCompanies) 
     ->setParameter('excludedStates', $excludedStates) 
     ->groupBy('e.company') 
     ->addGroupBy('e.state') 
     ->getQuery() 
     ->getResult() 
    ; 
+0

坦克!相信兩個名單的聯合「不在」不會結合領域。 @理查德 – jotamolas

+0

例如,如果我有「巴黎,頂點s.a.」和「倫敦,自由之家」當您輸入新行「paris,liberty s.a」時,查詢不會返回它。 – jotamolas