H
H
hobu4ok912016-06-07 10:26:59
Yii
hobu4ok91, 2016-06-07 10:26:59

How to select unique values ​​from database in yii2?

It is necessary to select values ​​for 2 fields (For example, code and name), lines where the "code" field is repeated, although the "name" field is different, should not be selected.
The result should look something like this:
Array [
[code => 1, name => a],
[code => 2, name => a],
[code => 3, name => b],
]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly Medvedev, 2016-06-07
@balamyt92

Something like that
$query->groupBy('code');

D
Dmitry Kim, 2016-06-07
@kimono

Try:

Model::find()->select(['code', 'name'], 'DISTINCT')->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question