使用postgres作为数据库的doctrine语法,是否可以生成如下的查询:
SELECT
DISTINCT ON (people.email)
*
FROM people
WHERE people.company SIMILAR TO '%companyA%|%companyB%|%...';
到目前为止,我有:
[...]
$query= Doctrine_Query::create()->from('People p')
->select('p.email, p.name, p.surname');
$alias= $query->getRootAlias();
foreach ($companies as $company){
$query->orWhere($alias.'.name ilike ?', '%'.$company.'%');
}
[...]
我对Doctrine ORM的“DISTINCT ON”感兴趣
由于
答案 0 :(得分:0)
似乎Docrine没有很好的方法来使用“DISTINCT ON”。另外我还没有找到适合我需要的“DISTINCT”或 - > distinct()的方法(当Doctrine构建SQL查询时,我得到了许多糟糕的东西)。
最后,我使用不同的方法完成了DISTINCT ON。这是我现在使用的SQL查询:
SELECT *
FROM people
WHERE id IN (SELECT min(id)
FROM people
WHERE company
SIMILAR TO '%companyA%|%companyB%|%...')
GROUP BY company;
我用来从symfony生成它的Doctrine语法或多或少:
$query = Doctrine_Query::create()->from('Company c')
->select('.....');
$innerquery = Doctrine_Query::create()->from('Company ic')
->select('MIN(ic.id) AS id'); // important the "AS id"
$innerquery = (... stuff to get the company A, B, C in an andWhere() ...)
$innerquery->groupBy('ic.company');
$query->Where('c.id IN ('.$innerquery.')');