$sql = "SELECT * FROM `l_company_distributor_message` WHERE id IN (SELECT MAX(id) FROM `l_company_distributor_message` WHERE distributor_id = ".$this->_distributorId." GROUP BY company_id )"; $message = Yii::$app->getDb()->createCommand($sql)->queryAll();