【问题标题】:How to add distinct on foreign key and primary key in Yii CdBCriteria如何在 Yii CdBCriteria 中添加不同的外键和主键
【发布时间】:2014-09-26 06:56:03
【问题描述】:

我有两个表 store(id,name,date)store_services(id,store_id,name,price,date) 我已经通过 price 实现了搜索过滤器,我想在我的页面中显示唯一的商店记录。 但我仍然从存储表中获取重复记录。

通过 distinct(store_services.store_id) 在 mysql 查询中运行良好 但它不适用于 Yii CDbCriteria。 我的 Mysql 查询是:

SELECT DISTINCT(store_services.store_id),store.id,store.name,store.date FROM store INNER JOIN store_services ON store.id = store_services.store_id WHERE store_service.price BETWEEN 1,1000

请给我一个 Yii 不同记录的代码 注意: 我在 Yii 中使用 $criteria->distinct=true;

但它也得到重复记录

【问题讨论】:

  • 删除 Distinct 并尝试在同一列上使用 group by,例如 $criteria->group = 'store_id';
  • 我建议在这里粘贴代码,确保您使用的是 findAll();
  • 我用我的完整代码来回答。你想看看。

标签: php mysql yii


【解决方案1】:

它正在将 $criteria->distinct=true; 替换为 $criteria->group = 'store_id';

这是我的所有代码,希望有人能从中找到帮助。

        $criteria=new CDbCriteria;
    //$criteria->distinct=true;
    $criteria->group = 'store_id';
    $criteria->select = 't.id,t.name,t.state,t.city,t.location,t.address,t.contact_no,t.email,t.facilities,t.profile_photo,t.description, t.merchant_id, t.approve, t.added_date';
    $flag = false;

    if(isset($_GET['Store']['category']) && !empty($_GET['Store']['category'])){
        $criteria->compare('mmmStoreServices.category_id',  $_GET['Store']['category']);
        $flag = true;
    }

    if(isset($_GET['Store']['sub_category']) && !empty($_GET['Store']['sub_category'])){
        $criteria->compare('mmmStoreServices.service_id', $_GET['Store']['sub_category']);
        $flag = true;
    }



    if(isset($_GET['Store']['price']) && !empty($_GET['Store']['price'])){
        $price = explode('-',$_GET['Store']['price']);
        $minPrice = trim($price[0]);
        $maxPrice = trim($price[1]);
        $criteria->addBetweenCondition('mmmStoreServices.price', $minPrice, $maxPrice);
        $flag = true;

    }


    if($flag){
        $criteria->with = array('mmmStoreServices'); // Put `mmm_store_service` to relations of model 'Store'
        $criteria->together = true; // Check if you really need this parameter!
    }

    if(isset($_GET['Store']['location']) && !empty($_GET['Store']['location'])){ 
        $criteria->compare('t.location', $_GET["Store"]["location"]);
        //$flag = true;

    }


    $criteria->compare('t.approve', 'Y');   

    $ajaxModel = new CActiveDataProvider('Store', array(
        'criteria' => $criteria,
        'pagination' => array('pageSize'=>'2'),
    ));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    相关资源
    最近更新 更多