Saurabh Saurabh - 1 year ago 527
MySQL Question

Yii2 : How to write distinct sql query?

Want to implement following SQL queries in YII 2 but no success-

SELECT count(DISTINCT(company_name)) FROM clients

This should give total number of Unique Company Name


SELECT (DISTINCT(company_name,client_code)) FROM clients

and this should display company_name with client code and

how to achieve this?

Answer Source

Answering my own question I got following working solutions-

Got the count for Unique company_name,

$query = new Query;
    $my = (new yii\db\Query())
  echo $my;   

And list of distinct company_name and client_code by -

$query = new yii\db\Query();
            $data = $query->select(['company_name','client_code'])
        if($data) {
         foreach($data as $row)
        echo 'company_name: ' . $row['company_name'] . ' client_code: ' . $row['client_code'] . '<br>';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download