2016-02-16 248 views
1

我已經建立了與Yii2查詢生成器下面的查詢:在Yii2結合GROUP_CONCAT DISTINCT與COUNT查詢

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

給我看一個正常的sql示例..所以我可以evaluete你的目標 – scaisEdge

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