在Yii2查询中将GROUP_CONCAT DISTINCT与COUNT组合在一起

时间:2016-02-16 16:10:39

标签: php jquery mysql yii2 distinct

我已经使用Yii2查询构建器构建了以下查询:

public function searchFilter() {
    $query = (new Query())
        ->select([
            'GROUP_CONCAT(DISTINCT(a.merk), ".", m.merk ORDER BY a.merk ASC)                merk',
            'GROUP_CONCAT(DISTINCT(b.brandstof) ORDER BY b.brandstof ASC)                   brandstof',
            'GROUP_CONCAT(DISTINCT(a.bouwjaar) ORDER BY a.bouwjaar DESC)                    bouwjaar',
            'GROUP_CONCAT(DISTINCT(k.kleur) ORDER BY k.kleur ASC)                           kleur',
            'GROUP_CONCAT(DISTINCT(t.transmissie) ORDER BY t.transmissie ASC)               transmissie',
        ])
        ->from('auto_new a')
        ->join('INNER JOIN', 'tbl_merken m', 'a.merk = m.merk_id')                          //Merk
        ->join('INNER JOIN', 'tbl_kleur k', 'a.kleur = k.kleur_id')                         //Kleur
        ->join('INNER JOIN', 'tbl_transmissie t', 'a.transmissie = t.transmissie_id')       //Transmissie
        ->join('INNER JOIN', 'tbl_brandstof b', 'a.brandstof = b.brandstof_id');            //Brandstof
    return $query;
}

这给了我以下输出:

image

但是现在我想在DISTINCT中的每个值之后添加一个计数。但我不知道该怎么做。

修改 这是执行查询的函数

public function actionSearchfilter($ac) {
    Yii::$app->response->format = Response::FORMAT_JSON;
    $query = Car::searchFilter()->where(['a.ac' => $ac, 'a.flag' => '1'])->all();
    return $query;
}

1 个答案:

答案 0 :(得分:0)

试试这个吗?

->select([
    'GROUP_CONCAT(DISTINCT(a.merk), ".", m.merk ORDER BY a.merk ASC)                merk',
    'GROUP_CONCAT(DISTINCT(b.brandstof) ORDER BY b.brandstof ASC)                   brandstof',
    'GROUP_CONCAT(DISTINCT(a.bouwjaar) ORDER BY a.bouwjaar DESC)                    bouwjaar',
    'GROUP_CONCAT(DISTINCT(k.kleur) ORDER BY k.kleur ASC)                           kleur',
    'GROUP_CONCAT(DISTINCT(t.transmissie) ORDER BY t.transmissie ASC)               transmissie',
])
->from('auto_new a')
->join('INNER JOIN', 'tbl_merken m', 'a.merk = m.merk_id')                          //Merk
->join('INNER JOIN', 'tbl_kleur k', 'a.kleur = k.kleur_id')                         //Kleur
->join('INNER JOIN', 'tbl_transmissie t', 'a.transmissie = t.transmissie_id')       //Transmissie
->join('INNER JOIN', 'tbl_brandstof b', 'a.brandstof = b.brandstof_id');              //Brandstof

->count();
return $query;