我正在研究symfony 1.0中的项目
我在创建推进查询方面遇到了问题。
我有两张桌子
车
id name
1 a
2 b
3 c
特征
id car_id feature
1 2 f1
2 2 f2
3 2 f3
4 1 f1
5 3 f3
6 3 f2
7 3 f4
这是SQLFiddle with table and value。
我有功能array(f1,f2,f3)
。现在我只想要具有所有三个特征的汽车,即在我们的情况下是“b”。
我已经尝试但没有给出满意的结果
$c = new Criteria();
$c->clearSelectColumn();
$c->addSelectColumn(CarPeer::NAME);
$c->addSelectColumn(FeaturePeer::CAR_ID);
$c->addJoin(CarPeer::ID,FeaturePeer::CAR_ID,Criteria::LEFT_JOIN);
$c->add(FeaturePeer::FEATURE,array(f1,f2,f3),Criteria::IN);
$c->addGroupBy(CarPeer::ID);
$resultset = CarPeer::doSelectRs($c);
答案 0 :(得分:1)
我相信这是你想要的SQl(似乎无论如何都在我的SQL Fiddle中工作):
SELECT car_id FROM
(SELECT COUNT(car_id) AS feature_count, car_id FROM feature
WHERE feature IN ('f1', 'f2', 'f3') GROUP BY car_id
) car_features
WHERE car_features.feature_count=3
现在要在Propel中得到它,你需要做类似的事情:
$features = array("f1", "f2", "f3");
$featureCount = FeatureQuery::create()
->withColumn("COUNT(car_id)", "feature_count")
->add(FeaturePeer::FEATURE, $features, Criteria::IN)
->addGroupBy(CarPeer::ID);
$cars = CarQuery::create()
->addSelectQuery($featureCount, "car_features")
->where("car_features.feature_count = ?", sizeof($features))
->find();