Mysql
 sql >> Database >  >> RDS >> Mysql

Yii2:Come scrivere una query SQL distinta?

Prova questo:

$total = YourModel::find()->select('company_name')->distinct()->count();

Nel modello di ricerca:

public function search($params)
{
    $query = YourModel::find()->select('company_name')->distinct();
    // or
    $query = YourModel::find()->select(['company_name', 'client_code'])->distinct();

    $query->orderBy('id desc');

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);
    // ...
}