1

我正在尝试使用推进标准构建以下查询:

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
  )
);

但查询确实返回任何结果。

我是否正确使用了自定义标准?

4

1 回答 1

1

乍一看,你count distinct似乎错了:

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

如果它只是一个糟糕的 c/p,关于这个片段,你可能会这样做:

$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));
于 2012-04-12T14:59:34.583 回答