【问题标题】:How to retrieve rows that has all features using Propel?如何使用 Propel 检索具有所有功能的行?
【发布时间】:2012-09-18 05:47:05
【问题描述】:

我正在开发 symfony 1.0 中的项目

我在创建推进查询时遇到问题。

我有两张桌子

  1. 汽车

    id     name
    1       a
    2       b
    3       c
    
  2. 功能

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

【问题讨论】:

    标签: php mysql symfony1 propel


    【解决方案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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 2014-11-18
      相关资源
      最近更新 更多