推动自定义标准

时间:2012-04-12 14:49:06

标签: mysql sql propel

我正在尝试使用propel critiera构建以下查询:

SELECT *
FROM mytable
WHERE facility_id IN (4,7)
GROUP BY housing_id
HAVING COUNT(DISTINCT facility_id) = 2;

推进标准:

$criteria->add(HousingsHousingFacilitiesPeer::HOUSING_FACILITY_ID, $facility_filter, Criteria::IN);

$criteria->addGroupByColumn(HousingsHousingFacilitiesPeer::HOUSING_ID);

$criteria->addHaving(
  $criteria->getNewCriterion(
    HousingsHousingFacilitiesPeer::HOUSING_ID, 
    'COUNT(DISTINCT housings_housing_facilities.HOUSING_FACILITY_ID) = 2',
    Criteria::CUSTOM
  )
);

但查询会净返回任何结果。

我是否正确使用自定义条件?

1 个答案:

答案 0 :(得分:1)

首先检查时,count distinct似乎错了:

'COUNT(DISTINCT HousingsHousingFacilitiesPeer::HOUSING_FACILITY_ID) = 2'

如果它只是一个糟糕的c / p,关于this snippet,您可能会这样做:

$c = new Criteria();
$c->add(HousingsHousingFacilitiesPeer::HOUSING_FACILITY_ID, $facility_filter, Criteria::IN);
$c->addGroupByColumn(HousingsHousingFacilitiesPeer::HOUSING_ID);
$c->addAsColumn('numFacilities', 'COUNT(DISTINCT '.HousingsHousingFacilitiesPeer::HOUSING_FACILITY_ID.')');
$c->addHaving($c->getNewCriterion(HousingsHousingFacilitiesPeer::ID, 'numArticles=2', Criteria::CUSTOM));