Yii2 : How to write distinct SQL query?

前端 未结 4 1764
遇见更好的自我
遇见更好的自我 2021-01-01 10:55

I want to implement following SQL queries in Yii 2 but with no success.

This should give total number of unique company names:



        
4条回答
  •  不知归路
    2021-01-01 11:40

    I hope this sample is useful for you

     $names = Yii::$app->db->createCommand('SELECT  count(DISTINCT(company_name)) as name FROM clients')
        ->queryAll();
    

    for access the the data

    foreach ($names as $name){
        echo $name['name'];
    }
    

提交回复
热议问题